标签:
(1)UIDynammic使用分三步:
——创建仿真器(顺便定义仿真范围)(可利用懒加载定义)
——创建仿真行为(顺便添加仿真元素)
——把仿真行为 添加到 仿真器 中
(2)下面是结合重力行为和碰撞行为的例子

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *rectView;
@property(nonatomic,strong) UIDynamicAnimator *ani;
@property (weak, nonatomic) IBOutlet UIView *blueView;
@end
@implementation ViewController
-(UIDynamicAnimator *)ani{
    if (!_ani) {
        //创建仿真器(顺便定义仿真范围)
        _ani=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    }
    return _ani;
}
- (void)viewDidLoad {
    [super viewDidLoad];
}
//blueView只参与碰撞仿真,不参与重力下落的仿真
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //创建仿真器(顺便定义仿真范围),在懒加载中
    //UIDynamicAnimator *ani=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    //创建仿真行为(顺便添加仿真元素)
    UIGravityBehavior *behave=[[UIGravityBehavior alloc]initWithItems:@[self.rectView]];
    //按照向量来定义方向
    behave.gravityDirection=CGVectorMake(1, 1);
    //按照角度定义方向
//    behave.angle=-M_PI_4;
    //定义重力加速度
    behave.magnitude=1;
    
    
    //碰撞检测,把仿真范围设为边界
    UICollisionBehavior *collisionBehave=[[UICollisionBehavior alloc]initWithItems:@[self.rectView,self.blueView]];
    collisionBehave.translatesReferenceBoundsIntoBoundary=YES;
    
    //仿真行为 添加到 仿真器 中
    [self.ani addBehavior:behave];
    [self.ani addBehavior:collisionBehave];
}
@end——碰撞比较重要的属性是有几种定义边界的方法。
如上面的translatesReferenceBoundsIntoBoundary属性。还有其他两种主要的:
    //碰撞检测,把仿真范围设为边界
    UICollisionBehavior *collisionBehave=[[UICollisionBehavior alloc]initWithItems:@[self.rectView,self.blueView]];
    collisionBehave.translatesReferenceBoundsIntoBoundary=YES;
    //添加一条左右倾斜的直线作为碰撞边界
    [collisionBehave addBoundaryWithIdentifier:@"line" fromPoint:CGPointMake(0, 300) toPoint:CGPointMake(320, 400)];
    //添加一个圆做边界
    UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 320, 320)];
    [collisionBehave addBoundaryWithIdentifier:@"circle" forPath:path];#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *pinkView;
@property(nonatomic,strong) UIDynamicAnimator *ani;
@end
@implementation ViewController
-(UIDynamicAnimator *)ani{
    if (_ani==nil) {
        _ani=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    }
    return _ani;
}
- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint point=[touch locationInView:touch.view];
    //创建捕捉行为
    UISnapBehavior *snap=[[UISnapBehavior alloc]initWithItem:self.pinkView snapToPoint:point];
    //设置捕捉属性
    //减震设置,0~1,值越大,减震越好,震动幅度就小。
    snap.damping=0.5;
    //删除所有行为(以保证每次点击都有效,否则第二次以后点击,没反应)
    [self.ani removeAllBehaviors];
    //添加行为
    [self.ani addBehavior:snap];
}——UIPushBehavior:推动行为
——UIAttachmentBehavior:附着行为
——UIDynamicItemBehavior:动力元素行为
【iOS开发-112】UIDynamic物理模拟介绍,如重力行为、碰撞行为、碰撞行为以及其他
标签:
原文地址:http://blog.csdn.net/weisubao/article/details/43225757