@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