@interface ViewController : UIViewController@end/*物理仿真元素 注意: 不是任何对象都能做物理仿真元素 不是任何对象都能进行物理仿真 物理仿真元素要素: 任何遵守了UIDynamicItem协议的对象 UIView默认已经遵守了UIDynamicItem协议,因此任何UI控件都能做物理仿真 UICollectionViewLayoutAttributes类默认也遵守UIDynamicItem协议 */#import "ViewController.h"@import QuartzCore;const CGPoint kInitialPoint1 = { .x = 33, .y = 250 };const CGPoint kInitialPoint2 = { .x = 150, .y = 250 };@interface ViewController ()@property (weak, nonatomic) IBOutlet UIView *box1;@property (weak, nonatomic) IBOutlet UIView *box2;@property (nonatomic) UIDynamicAnimator *dynamicAnimator;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.dynamicAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; [self reset];}- (IBAction)reset { [self.dynamicAnimator removeAllBehaviors]; self.box1.center = kInitialPoint1; self.box1.layer.cornerRadius = 10; self.box1.layer.borderWidth = 2; self.box1.layer.borderColor = [[UIColor blackColor]CGColor]; self.box1.transform = CGAffineTransformIdentity; self.box2.center = kInitialPoint2; self.box2.layer.cornerRadius = 10; self.box2.layer.borderWidth = 2; self.box2.layer.borderColor = [[UIColor greenColor]CGColor]; self.box2.transform = CGAffineTransformIdentity;}- (IBAction)snap {//迅速移动行为(捕捉行为) CGPoint point = [self randomPoint]; UISnapBehavior/*物理仿真行为*/ *snap = [[UISnapBehavior alloc] initWithItem:self.box1//物理仿真元素 要做动画的元素 snapToPoint:point]; snap.damping = 0.25;//阻尼系数 移动后的弹动幅度 [self addTemporaryBehavior:snap];}- (IBAction)attach {//附着行为 ??? UIAttachmentBehavior *attach1 = [[UIAttachmentBehavior alloc] initWithItem:self.box1 offsetFromCenter:UIOffsetMake(50, 50)//旋转点 attachedToAnchor:self.box1.center]; [self.dynamicAnimator addBehavior:attach1]; UIAttachmentBehavior *attach2 = [[UIAttachmentBehavior alloc] initWithItem:self.box2 attachedToItem:self.box1]; [self.dynamicAnimator addBehavior:attach2]; UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.box2] mode:UIPushBehaviorModeInstantaneous]; push.pushDirection = CGVectorMake(0, 2); [self.dynamicAnimator addBehavior:push]; }- (IBAction)push {//推动行为 UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.box1] mode:UIPushBehaviorModeContinuous]; push.pushDirection = CGVectorMake(1, 1);// CGVectorMake(0垂直方向 数值越大越往右偏移, 推动速度) [self.dynamicAnimator addBehavior:push];}- (IBAction)gravity {//重力行为 UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.box1]]; gravity.action = ^{ NSLog(@"%@", NSStringFromCGPoint(self.box1.center)); }; [self.dynamicAnimator addBehavior:gravity];}- (IBAction)collision {//碰撞行为 UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.box1, self.box2]]; [self.dynamicAnimator addBehavior:collision]; UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.box1] mode:UIPushBehaviorModeInstantaneous]; push.pushDirection = CGVectorMake(3, 0); collision.action = ^{//行为中添加操作 if (self.box2.center.x-self.box2.frame.size.width/2 > self.view.frame.size.width){ [self reset]; } }; [self addTemporaryBehavior:push]; }- (void)addTemporaryBehavior:(UIDynamicBehavior *)behavior { [self.dynamicAnimator/*物理仿真器 实施行为的引擎*/ addBehavior:behavior]; [self.dynamicAnimator performSelector:@selector(removeBehavior:) withObject:behavior afterDelay:1];}- (CGPoint)randomPoint { CGSize size = self.view.bounds.size; return CGPointMake(arc4random_uniform(size.width), arc4random_uniform(size.height));}@end