-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[self.redView]];
gravity.gravityDirection = CGVectorMake(1.0, 1.0);
UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[self.redView,self.greenView ]];
collision.translatesReferenceBoundsIntoBoundary = YES;
// 给碰撞添加边界
CGPoint startPoint = CGPointMake(0, self.view.frame.size.height-100);
CGPoint endPoint = CGPointMake(self.view.frame.size.width, self.view.frame.size.height-100);
// [collision addBoundaryWithIdentifier:@"123" fromPoint:startPoint toPoint:endPoint];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 200, 200 , 300)];
[collision addBoundaryWithIdentifier:@"321" forPath:path];
// 移除边界用的标示 @“123”
// 将仿真行为添加到仿真器中
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
}
-(UIDynamicAnimator*)animator{
if (_animator == nil) {
_animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
}
return _animator;
}
1.2 捕捉行为
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
// 移除里面的行为
[self.animator removeAllBehaviors];
CGPoint point = [[ touches anyObject] locationInView:self.view];
// 创建捕捉行为
UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:self.redView snapToPoint:point];
// 设置阻力系数(0-1.0)
snap.damping = 0;
//添加行为
[self.animator addBehavior:snap];
}
网友评论