1

I'm trying to implement the behaviour seen in the iOS lock screen when touching the camera icon, using a UIPageViewController

I have a UIPageViewController that vertically scrolls between 2 view controllers. I would like to do a small "bounce/bump" when the user taps the view.

Any ideas on how I can accomplish this?

enter image description here

Thank you!!

nmdias
  • 3,888
  • 5
  • 36
  • 59
  • 2
    I think the easiest way to acomplish such efect is to use UIKit Dynamics. This post may help you a bit: http://stackoverflow.com/questions/21892105/how-to-create-a-uiview-bounce-animation – Szu Sep 24 '14 at 10:28
  • Thank you! That seems (is) to be the right trail :) – nmdias Sep 24 '14 at 11:24
  • No problem :-) If you find an answer please write here your solution please. – Szu Sep 24 '14 at 11:50

1 Answers1

2

Thanks to @Szu for pointing me out to UIKit Dynamics :)

Here's a rough solution:

Call init with a controller's view (to be bounced) and a reference view (your current view controller's view property).

eg.

FSBounceAnimator(contentView: viewController.view, referenceView: self.view)

viewController is any viewController that you may instantiated and added as a childViewController

self.view is your current viewController view.

Call bounce(), to bounce :)

import UIKit

class FSBounceAnimator: NSObject {

    var animator: UIDynamicAnimator
    var gravityBehaviour: UIGravityBehavior
    var pushBehavior: UIPushBehavior
    var itemBehaviour: UIDynamicItemBehavior

    init(contentView: UIView!, referenceView: UIView!) {

        animator = UIDynamicAnimator(referenceView: referenceView)

        var colisionBehaviour: UICollisionBehavior = UICollisionBehavior(items: [contentView])
        colisionBehaviour.setTranslatesReferenceBoundsIntoBoundaryWithInsets(UIEdgeInsetsMake(-100, 0, 0, 0))
        animator.addBehavior(colisionBehaviour)

        gravityBehaviour = UIGravityBehavior(items: [contentView])
        gravityBehaviour.gravityDirection = CGVectorMake(1, 1)
        animator.addBehavior(gravityBehaviour)

        pushBehavior = UIPushBehavior(items: [contentView], mode: UIPushBehaviorMode.Instantaneous)
        pushBehavior.magnitude = 0.0
        pushBehavior.angle = 0.0
        animator.addBehavior(pushBehavior)

        itemBehaviour = UIDynamicItemBehavior(items: [contentView])
        itemBehaviour.elasticity = 0.45
        animator.addBehavior(itemBehaviour)

        super.init()
    }

    func bounce() {
        self.pushBehavior.pushDirection = CGVectorMake(0.0, 100.0);
        self.pushBehavior.active = true;
    }

}
nmdias
  • 3,888
  • 5
  • 36
  • 59