【iOS开发-112】UIDynamic物理模拟介绍,如重力行为、碰撞行为、碰撞行为以及其他

(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

——重力行为的3个重要属性,两个方向属性 gravityDirectionangle,一个加速度属性 magnitude

——碰撞比较重要的属性是有几种定义边界的方法。

如上面的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];

(3)捕捉行为,主要属性是减震属性damping。

#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];
}

(4)除以上3种行为之外,还有其他行为:

——UIPushBehavior:推动行为

——UIAttachmentBehavior:附着行为

——UIDynamicItemBehavior:动力元素行为


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值