// 1. UIDynamicAnimator来负责仿真
animator = [[UIDynamicAnimator alloc] initWithReferenceView:view];
// 1.自由落体
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:views];
[gravity setAngle:M_PI_2 magnitude:0.1];
// 开始仿真
[animator addBehavior:gravity];
// 2.碰撞检测
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:views];
// 设置不要出边界,碰到边界会被反弹
collision.translatesReferenceBoundsIntoBoundary = YES;
collision.collisionMode = UICollisionBehaviorModeBoundaries;
// 设置碰撞代理(根据需要设置)
collision.collisionDelegate = self;
// 开始仿真
[animator addBehavior:collision];
// 3.物体的属性行为
UIDynamicItemBehavior *itemProperty = [[UIDynamicItemBehavior alloc] initWithItems:views];
// 设置物体弹性,振幅
// 动态弹性量
itemProperty.elasticity = 1.0;
// 物体间的摩擦力
itemProperty.friction = 0;
// 相对密度
itemProperty.density = 0.1;
// 线速度阻力(随时间推移使得线性速度下降的阻力)
itemProperty.resistance = 0.0;
// 角速度阻力
itemProperty.angularResistance = 0;
// 相互作用力
itemProperty.charge = 0;
[animator addBehavior:itemProperty];
// 4.添加推动行为
UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:views mode:UIPushBehaviorModeInstantaneous];
// 力的大小
push.magnitude = 0.05;
// 力的方向,水平正向x = 0.5, 垂直正向y = 0.5
push.pushDirection = CGVectorMake(0, 0.5);
[animator addBehavior:push];
#pragma mark - UICollisionBehaviorDelegate
// 动态物体与边界碰撞
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p {
}
网友评论