1.
创建仿真者
ReferenceView:
仿真区域
(
参考系
)
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
2.
创建仿真行为 重力行为
UIGravityBehavior * gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
2.1
设置重力方向
方式一
(
包括方向
和大小
)
gravity.gravityDirection = CGVectorMake(10, 10);
方式二
设置方向
gravity. angle = M_PI_4 ;
gravity. angle = M_PI_4 ;
设置大小
1000 points/second²
gravity.
magnitude
=
10
;
3.
把仿真行为添加到仿真者中
[self.animator addBehavior:gravity];
2.
创建仿真行为
(
碰撞行为
)
UICollisionBehavior * collision = [[ UICollisionBehavior alloc ] initWithItems : @[ self . redView ] ];
UICollisionBehavior * collision = [[ UICollisionBehavior alloc ] initWithItems : @[ self . redView ] ];
设置
YES
就是把仿真区域转化为碰撞的边界
collision.translatesReferenceBoundsIntoBoundary = YES;
弹性系数
(0 ---- 1)
item.elasticity = 0;
2.
创建仿真行为
(
吸附
)
UISnapBehavior * snap = [[ UISnapBehavior alloc ] initWithItem : self . redView snapToPoint :locP];
UISnapBehavior * snap = [[ UISnapBehavior alloc ] initWithItem : self . redView snapToPoint :locP];
阻尼系数
(0 --- 1) 0:
最抖动
1:
最不抖动
snap.damping = 0;
附着行为
- (instancetype)initWithItem:(id <UIDynamicItem>)item attachedToAnchor:(CGPoint)point;
元素附着到点
- (instancetype)initWithItem:(id <UIDynamicItem>)item offsetFromCenter:(UIOffset)offset attachedToAnchor:(CGPoint)point NS_DESIGNATED_INITIALIZER;
元素附着到指定点
(
根据
center
偏移
)
- (instancetype)initWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2;
元素之间的附着
- (instancetype)initWithItem:(id <UIDynamicItem>)item1 offsetFromCenter:(UIOffset)offset1 attachedToItem:(id <UIDynamicItem>)item2 offsetFromCenter:(UIOffset)offset2 NS_DESIGNATED_INITIALIZER;
指定元素之间的附着点
(
根据每个元素的
center
偏移
)
2.
创建仿真行为
(
附着行为
)
UIAttachmentBehavior * attachment = [[UIAttachmentBehavior alloc] initWithItem:self.redView attachedToAnchor:locP];
获取红色
View
的
size
CGSize
reSize =
self
.
redView
.
bounds
.
size
;
UIAttachmentBehaviorTypeItems,
元素之间的附着
UIAttachmentBehaviorTypeAnchor
元素和点的附着
附着行为 (刚性附着 弹性附着)
UIAttachmentBehavior * attachment = [[ UIAttachmentBehavior alloc ] initWithItem : self . redView offsetFromCenter : UIOffsetMake (-reSize. width / 2 , -reSize. height / 2 ) attachedToAnchor :locP];
设置线长
attachment.length = 100;
CZView * view = ( CZView *) self . view ;
设置起点和终点
attachment. action = ^{
设置起点
view. startPoint = locP;
设置终点
view.endPoint = self.redView.center;
转换坐标点
view. endPoint = [ self . view convertPoint : CGPointZero fromView : self . redView ];
};
设置弹性附着
(
同时设置以下两个属性
)
频率
attachment. frequency = 0.5 ;
阻尼系数
频率
attachment. frequency = 0.5 ;
阻尼系数
attachment.damping = 0.5;
2.
创建仿真行为 平推
UIPushBehaviorModeContinuous,
持续
UIPushBehaviorModeInstantaneous
瞬时
一次性
UIPushBehavior * push = [[
UIPushBehavior
alloc]
initWithItems:
@[
self.
redView
]
mode:
UIPushBehaviorModeInstantaneous];