UIDynamicBehavior(物理仿真行为)

由上篇文章,我们可知物理仿真行为有多种,不过使用起来都大同小异,下面我就挑其中三种进行介绍。

一、捕捉行为(UISnapBehavior)

可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动。

1.UISnapBehavior的初始化

   - (instancetype)initWithItem:(id <UIDynamicItem>)item
snapToPoint:(CGPoint)point;

2.UISnapBehavior常见属性

// 用于减幅、减震(取值范围是0.0 ~ 1.0,值越大,震动幅度越小)
@property (nonatomic, assign) CGFloat damping;

3.UISnapBehavior使用注意
如果要进行连续的捕捉行为,需要先把前面的捕捉行为从物理仿真器中移除

4.代码演示

- (void)test1 {
    // 1.获取当前触摸的手指
    UITouch *touch = [touches anyObject];
    // 2.更具手指取出位置
    CGPoint point = [touch locationInView:touch.view];
    
    // 吸附行为
    // 1.创建物理仿真器
    // 2.创建物理捕捉行为
    UISnapBehavior *snapB = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:point];
    
    // 3.设置吸附行为的"减震"
    snapB.damping = 0;
    
    // 4.注意: 吸附行为默认只能吸附一次, 如果多次吸附必须从仿真器中移除再重新添加
    [self.anim removeAllBehaviors];
    
    // 5.将物理仿真行为添加到仿真器中, self.dynamicAnimator为懒加载的物理仿真器对象
    [self.dynamicAnimator addBehavior:snapB];
}

二、重力行为(UIGravityBehavior)

给定重力方向、加速度,让物体朝着重力方向掉落。
1.UIGravityBehavior的初始化
- (instancetype)initWithItems:(NSArray *)items;
item参数 :里面存放着物理仿真元素

2.UIGravityBehavior常见方法

// 添加1个物理仿真元素
- (void)addItem:(id <UIDynamicItem>)item;

// 移除1个物理仿真元素
- (void)removeItem:(id <UIDynamicItem>)item;
 
  1. UIGravityBehavior常见属性
// 添加到重力行为中的所有物理仿真元素
@property (nonatomic, readonly, copy) NSArray* items;
 
// 重力方向(是一个二维向量)
@property (readwrite, nonatomic) CGVector gravityDirection;

// 重力方向(是一个角度,以x轴正方向为0°,顺时针正数,逆时针负数)
 @property (readwrite, nonatomic) CGFloat angle;

// 量级(用来控制加速度,1.0代表加速度是1000 points /second²)
@property (readwrite, nonatomic) CGFloat magnitude;

4.代码演练

- (void)test2 {
    // 演示重力行为
    // 1.创建物理仿真器
    // 并且指定了当前控制器的view作为仿真范围
    //   UIDynamicAnimator *anim = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    
    // 2.创建物理仿真行为
    // 并且指定红色为作为仿真元素
    UIGravityBehavior *gravityB = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
    
    // 设置重力的方向
    //    gravityB.gravityDirection = CGVectorMake(1, 0);
    //    gravityB.gravityDirection = CGVectorMake(0, -1);
     gravityB.gravityDirection = CGVectorMake(1, 1);

    // 设置重力的角度
     gravityB.angle = M_PI_2;
    
      // 设置重力的加速度
    gravityB.magnitude = 100.0;
    
    // 将物理仿真行为添加到仿真器中, self.dynamicAnimator为懒加载的物理仿真器对象
    [self.dynamicAnimator addBehavior:gravityB];
}

三、碰撞行为(UICollisionBehavior)

可以让物体之间实现碰撞效果;可以通过添加边界(boundary),让物理碰撞局限在某个空间中。

1、UICollisionBehavior常见的方法

- (void)addBoundaryWithIdentifier (id<NSCopying>)identifier
forPath:(UIBezierPath*)bezierPath;

- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;

- (UIBezierPath*)boundaryWithIdentifier:(id <NSCopying>)identifier;
- (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier;

- (void)removeAllBoundaries;

- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;

// 设置参照视图的bounds为边界,并且设置内边距
-(void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;

2.UICollisionBehavior常见属性

@property (nonatomic, readonly, copy) NSArray* boundaryIdentifiers;

// 是否以参照视图的bounds为边界
@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;

// 碰撞模式(分为3种,元素碰撞、边界碰撞、全体碰撞)
@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;

// 代理对象(可以监听元素的碰撞过程)
@property (nonatomic, assign, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;

注意:
碰撞行为要结合重力行为,才有效果。

3.代码演练

- (void)test3 {
    // 碰撞
    // 创建物理仿真器
    // 创建物理仿真行为
    UIGravityBehavior *gravigtyB = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
    //    gravigtyB.magnitude = 100;
    
    // 创建碰撞仿真行为
    UICollisionBehavior *collisionB = [[UICollisionBehavior alloc] initWithItems:@[self.redView, self.st]];
    // 设置碰撞的边界
    //    collisionB.translatesReferenceBoundsIntoBoundary = YES;
    
    // 添加直线边界
    //    [collisionB addBoundaryWithIdentifier:@"line" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(320, 420)];
    
    // 添加图形的边界
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.view.frame];
    [collisionB addBoundaryWithIdentifier:@"abc" forPath:path];
    
    
    // 5.将物理仿真行为添加到仿真器中,self.dynamicAnimator为懒加载的物理仿真器对象
    [self.dynamicAnimator addBehavior:gravigtyB];
    [self.dynamicAnimator addBehavior:collisionB];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容