Explanation of Collision Effect of iOS mechanics animation generator UIKit Dynamics, uikitdynamics

Source: Internet
Author: User

Explanation of Collision Effect of iOS mechanics animation generator UIKit Dynamics, uikitdynamics

UIKit Dynamic is a new set of classes and Methods Added by iOS7. It can give UIView lifelike behaviors and features without the need to write tedious code for animation effects, this allows developers to easily improve the user experience of applications. There are a total of six classes that can be used to customize UIDynamicAnimator. Here we will only briefly introduce the collision animation effect, that is, UICollisionBehavior. You need to sign a proxy agreement: UICollisionBehaviorDelegate. Other effects, coming soon.

Note: Remember to initialize dragonImageView and frogImageView: UIDynamicAnimator, UIGravityBehavior, and UICollisionBehavior must be declared as attributes or global variables. Otherwise, no animation effect will be produced!

 

# Import "ICFCollisionViewController. h"

@ Interface ICFCollisionViewController ()

{

UIImageView * dragonImageView;

UIImageView * frogImageView;

}

@ End

 

@ Implementation ICFCollisionViewController

 

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil

{

Self = [superinitWithNibName: nibNameOrNilbundle: nibBundleOrNil];

If (self ){

}

Return self;

}

 

-(Void) viewDidLoad

{

[SuperviewDidLoad];

 

// Initialize the animation generator.

UIDynamicAnimator * animator = [[UIDynamicAnimatoralloc] initWithReferenceView: self. view];

// Create an object for gravity falling Effect

UIGravityBehavior * gravityBehavior = [[UIGravityBehavioralloc] initWithItems: @ [frogImageView, dragonImageView];

 

// Set the acceleration of gravity in both the horizontal and vertical directions

[GravityBehavior setGravityDirection: CGVectorMake (0.0f, 0.3f)];

// Create the Collision Effect object

UICollisionBehavior * collisionBehavior = [[UICollisionBehavioralloc] initWithItems: @ [frogImageView, dragonImageView];

 

// There are three collision modes for objects: UICollisionBehaviorModeItems (collision between objects)

UICollisionBehaviorModeBoundaries)

UICollisionBehaviorModeEverything (collision with objects and boundary)

[CollisionBehavior setCollisionMode: UICollisionBehaviorModeEverything];

// This Code uses the border of self. view as the collision boundary and must be set to YES; otherwise, no collision effect will be generated.

CollisionBehavior. translatesreferencebounds=boundary = YES;

[AnimatoraddBehavior: gravityBehavior];

[AnimatoraddBehavior: collisionBehavior];

CollisionBehavior. collisionDelegate = self;

}

 

(Void) collisionBehavior :( UICollisionBehavior *) behavior beganContactForItem :( id <UIDynamicItem>) item withBoundaryIdentifier :( id <NSCopying>) identifier atPoint :( CGPoint p)

{

NSLog (@ "method triggered when a collision starts ");

}

 

-(Void) collisionBehavior :( UICollisionBehavior *) behavior endedContactForItem :( id <UIDynamicItem>) item withBoundaryIdentifier :( id <NSCopying>) identifier

{

NSLog (@ "method triggered when the collision ends ");

}

 

@ End

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.