1。可给活动项指派不同的物理特性
- (instancetype)initWithItems:(NSArray *)items //初始化
2。属性
allowsRotation
resistance: 抗阻力 0~CGFLOAT_MAX ,阻碍原有所加注的行为(如本来是重力自由落体行为,则阻碍其下落,阻碍程度根据其值来决定)
friction: 磨擦力 0.0~1.0 在碰撞行为里,碰撞对象的边缘产生
elasticity:弹跳性 0.0~1.0
density:密度 0~1
e.g.
- (UIView *) newViewWithCenter:(CGPoint)paramCenter backgroundColor:(UIColor *)paramBackgroundColor{
UIView *newView = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
newView.backgroundColor = paramBackgroundColor;
newView.center = paramCenter;
return newView;
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIView *topView = [self newViewWithCenter:CGPointMake(100.0f, 0.0f)
backgroundColor:[UIColor greenColor]];
UIView *bottomView = [self newViewWithCenter:CGPointMake(100.0f, 50.0f)
backgroundColor:[UIColor redColor]];
[self.view addSubview:topView];
[self.view addSubview:bottomView];
//构造动画
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//gravity
UIGravityBehavior *gravity = [[UIGravityBehavior alloc]
initWithItems:@[topView, bottomView]];
[self.animator addBehavior:gravity];
//collision
UICollisionBehavior *collision = [[UICollisionBehavior alloc]
initWithItems:@[topView, bottomView]];
collision.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collision];
//指派不同特性值 弹性bounce
UIDynamicItemBehavior *moreElasticItem = [[UIDynamicItemBehavior alloc]
initWithItems:@[bottomView]];
moreElasticItem.elasticity = 1.0f;
UIDynamicItemBehavior *lessElasticItem = [[UIDynamicItemBehavior alloc]
initWithItems:@[topView]];
lessElasticItem.elasticity = 0.5f;
[self.animator addBehavior:moreElasticItem];
[self.animator addBehavior:lessElasticItem];
}