UIDynamic的基本使用


一.使用步骤
1.创建仿真者
UIDynamicAnimator *animaytor = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];//后面的参数是:参照哪个View
2.添加要仿真的动作或者行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[redView,blueView]];
//添加碰撞检测行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[redView,blueView]];
3.将动作添加到仿真者中
[animaytor addBehavior:collision];
collision.translatesReferenceBoundsIntoBoundary = YES;
将动作添加到仿真者中
[animaytor addBehavior:gravity];
_animator = animaytor;//必须定义一个成员变量保存住仿真者,否则出了这个方法仿真者就不存在了

 

============================================================================
1.添加吸附动作 UISnapBehavior ------->吸附动作必须清除仿真者之前的行为
UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.box snapToPoint:location];//location是目标点
[self.animator addBehavior:snap];
2.推动作 UIPushBehavior
1>推行为必须设置3个属性 --->偏移量 //计算偏移量CGPoint offset = CGPointMake(_FristPoint.x - endPoint.x, _FristPoint.y - endPoint.y);
1)距离 也就是推行为的距离 --->按下屏幕的x,y -离开屏幕的x,y是偏移量,然后再用偏移量的(x*x+y*y)在开方,就是要求的距离,具体参考三角形求斜边公式
2)角度 ----->CGFloat angle = atan2f(offSet.y, offSet.x)
3) _push.active = YES;
3.刚性动作
1> 刚性行为初始化的时候需要指定2个点
1)定位点
2)锚点
4.弹性刚性行为----->和刚性动作大部分一样
多两个属性要设置
1)damping 振幅
2)frequency 频率
5.碰撞检测 ---->UICollisionBehavior
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[blueView]];
Boundary是即参与碰撞,又不会发生位移的静态物体的边界
[collision addBoundaryWithIdentifier:@"lalala" fromPoint:redView.frame.origin toPoint:CGPointMake(toX, toY)];
#pragma mark - 碰撞的代理方法
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值