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