标签:
UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性。
UIAttachmentBehavior(吸附),UICollisionBehavior(碰撞),UIGravityBehavior(重力),UIPushBehavior(推动),UISnapBehavior(捕捉)。另外还有一个辅助的行为UIDynamicItemBehavior,用来在item层级设定一些参数,比如item的摩擦,阻力,角阻力,弹性密度和可允许的旋转等等。
1.UIAttachmentBehavior(吸附):
- (void)viewDidLoad { [super viewDidLoad]; self.dynamicAnimation = [[UIDynamicAnimator alloc]initWithReferenceView:self.view]; // 图片 self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100,100)]; self.imgView.image = [UIImage imageNamed:@"dizhijieyong_icon"]; [self.view addSubview:self.imgView]; self.imgView.userInteractionEnabled = YES; // 添加手势 UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestAction:)]; [self.imgView addGestureRecognizer:panGest]; // 吸附 offset可以设置吸附的偏移,anchor是设置锚点 self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.imgView offsetFromCenter:UIOffsetMake(100, 120) attachedToAnchor:CGPointMake(100, 100)]; } // 实现一个pan手势,让一个image跟着手势跑 - (void)panGestAction: (UIPanGestureRecognizer *)panGest{ NSLog(@"手势触发啦!!!"); // 取得location CGPoint location = [panGest locationInView:self.view]; CGPoint boxLocation = [panGest locationInView:self.imgView]; switch (panGest.state) { // 开始拖拽 case UIGestureRecognizerStateBegan: NSLog(@"you touch started position %@",NSStringFromCGPoint(location)); NSLog(@"location in image started is %@",NSStringFromCGPoint(boxLocation)); // 移除 [self.dynamicAnimation removeAllBehaviors]; UIOffset offSet = UIOffsetMake(boxLocation.x - CGRectGetMidX(self.imgView.bounds), boxLocation.y - CGRectGetMidY(self.imgView.bounds)); self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.imgView offsetFromCenter:offSet attachedToAnchor:location]; self.attachment.damping=0.5; self.attachment.frequency=0.8; [self.dynamicAnimation addBehavior:self.attachment]; break; case UIGestureRecognizerStateEnded: [self.dynamicAnimation removeAllBehaviors]; default: [self.attachment setAnchorPoint:location]; break; } }
2.UIGravityBehavior(重力):
// 实现点击屏幕掉落一张图片的代码 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ self.gravity = [[UIGravityBehavior alloc]initWithItems:nil]; [self.dynamicAnimation addBehavior:self.gravity]; UIImageView *gravImagView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 50, 50)]; gravImagView.image = [UIImage imageNamed:@"zichanlingyong_icon"]; [self.view addSubview:gravImagView]; gravImagView.frame = CGRectMake(100, 100, 100, 100); [self.gravity addItem:gravImagView]; }
3.UISnapBehavior(捕捉):
// UISnapBehavior 将UIView通过动画吸附到某个点上 - (void) handleTap:(UITapGestureRecognizer *)paramTap{ CGPoint tapPoint = [paramTap locationInView:self.view]; if (self.snap != nil){ [self.dynamicAnimation removeBehavior:self.snap]; } self.snap = [[UISnapBehavior alloc] initWithItem:self.imgView snapToPoint:tapPoint]; self.snap.damping = 0.5f; //剧列程度 [self.dynamicAnimation addBehavior:self.snap]; }
还有一些没有试过,有时间补充吧!!!嘻嘻~~~~~
iOS开发UIkit动力学UIDynamicAnimator一系列动画
标签:
原文地址:http://www.cnblogs.com/pengsi/p/5798312.html