Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Bounce effect on View

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 618
    Comment on it

    iOS has integrated full physics engine to UIKit. Which not only integrate physics-related capabilities but adds smooth and fascinating animations by using underlying iOS Physics engine and provided dynamic items what we send in API. One can use the UIDynamicAnimator to have smooth bounce effect. 


    Here I’m going to tell you step-by-step to achieve the bouncy effects on UIObject(“titleLabel” in this example)
     

    1- create global variable of type UIDynamicAnimator &  UIGravityBehavior:

    var viewAnimator: UIDynamicAnimator!
            var gravityBehavior: UIGravityBehavior!
    

     

    2- Now, in the function where you want to execute the bounce effects, initialise the animator(created above) with a reference view(Say self.view or superView).

    viewAnimator = UIDynamicAnimator(referenceView: self.view)

     

    3-  Now initialise the gravityBehavior(created in step2) with items(using titleLabel in this example).

    gravityBehavior = UIGravityBehavior(items: [titleLabel])

     

    4- add gravity behaviour to animator.

    viewAnimator(gravityBehavior)

     

    5- Now, set boundaries using UICollisionBehavior with items.

    let collisionBehavior = UICollisionBehavior(items: [titleLabel])	     

     

    6- Make sure boundary based on the reference is active, make it true otherwise.

    collisionBehavior.translatesReferenceBoundsIntoBoundary = true;

     

    7- add the collision to animator.

    animator.addBehavior(collisionBehavior)

     

    8- similarly, create and add elasticity to the animator:

    let elasticity = UIDynamicItemBehavior(items: [titleLabel])
       	elasticity.elasticity = 0.7
       	animator.addBehavior(elasticity

     

    9- Run the code, You’ll observe smooth and fascinating bounce effect of your supplied view over which you want bounce effect.

    Code Snippet:

    var viewAnimator: UIDynamicAnimator!
    var gravityBehavior: UIGravityBehavior!
        
    @IBAction func addBounceAnimation(sender:UIButton?) {
            
            viewAnimator = UIDynamicAnimator(referenceView: self.view)
            gravityBehavior = UIGravityBehavior(items: [titleLabel])
            viewAnimator.addBehavior(gravityBehavior)
            let collisionBehavior = UICollisionBehavior(items: [titleLabel])
            collisionBehavior.translatesReferenceBoundsIntoBoundary = true;
            viewAnimator.addBehavior(collisionBehavior)
            let elasticity = UIDynamicItemBehavior(items: [titleLabel])
            elasticity.elasticity = 0.7
            viewAnimator.addBehavior(elasticity)
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: