UIKit力学(Dynamics)

UIKit力学是基于Box2D开源物理引擎

  • UIGravityBehavior 重力
  • UICollisionBehavior 碰撞,可以碰撞检测
  • UIAttachmentBehavior 吸附,互相吸附。两个物体之间的距离是刚性的
  • UIPushBehavior 推,向某个方向运动
  • UISnapBehavior 甩,朝某个目标点甩出
  • UIDynamicItemBehavior 行为限制。它含有的设置行为力学的参数:
    • density:密度:如果是一个100 * 100的物体,密度为1.0,作用力是1.0,加速度是100点/s²
    • elasticity:弹力系数,取值范围是0-1,0表示没有反弹,1.0表示完全弹性碰撞
  • friction:摩擦系数, 0表示没有摩擦力,1.0表示摩擦力很强,需要摩擦力很大可以大于1.0
  • resistance:阻力,物体运动的时候,在线性方向的阻力,0.0没有阻力,CGFLOAT_MAX表示最大阻力
  • allowsRotation:是否允许旋转
  • angularResistance:角阻力:物体旋转的时候,旋转方向的阻力

用代码说话

重力、碰撞、吸附、推

先看效果:


UIKit力学(Dynamics)_第1张图片

在SB中创建一个盒子、一个小圆球、一根木棍


UIKit力学(Dynamics)_第2张图片
Paste_Image.png
/**
 *  盒子重力加速度下降
 *  盒子在碰到棍子后弹起翻转
 *  盒子碰到球吸附
 */

@interface ViewController () {
    BOOL _firstContact; // 记录方块是否是第一次碰撞
}
@property (weak, nonatomic) IBOutlet UIView *box; // 盒子
@property (weak, nonatomic) IBOutlet UIImageView *line; // 木棍
@property (weak, nonatomic) IBOutlet UIImageView *ball; // 球


@property (nonatomic, retain) UIDynamicAnimator * animator;
@property (nonatomic, retain) UIGravityBehavior * gravity; // 重力
@property (nonatomic, retain) UICollisionBehavior * collision; // 碰撞
@property (nonatomic, retain) UIAttachmentBehavior * attachment; // 吸附

@end

@implementation
ViewController

- (void)viewDidLoad {
    _ball.layer.masksToBounds = YES;
    _ball.layer.cornerRadius = _ball.frame.size.width / 2;

    self.animator= [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    // 重力
     self.gravity= [[UIGravityBehavior alloc] initWithItems:@[_box]];

    // 设置重力的方向
//    CGVector gravityDirection = {0.0, 5}; // 如果y为负向上运动,值越大速度越大
//    _gravity.gravityDirection = gravityDirection;
    [_animator addBehavior:_gravity]; // 添加到animator中

    // 碰撞
    self.collision= [[UICollisionBehavior alloc] initWithItems:@[_box]];
    _collision.translatesReferenceBoundsIntoBoundary= YES; // 检测发生碰撞
    [_animator addBehavior:_collision];

    // 检测是否与其他视图边界进行碰撞
    [_collision addBoundaryWithIdentifier:@"collision"fromPoint:_line.frame.origin toPoint:CGPointMake(_line.frame.origin.x+ _line.frame.size.width, _line.frame.origin.y)];

    _collision.collisionDelegate = self; // 设置代理

    // 参数
    UIDynamicItemBehavior* item = [[UIDynamicItemBehavior alloc] initWithItems:@[_box]];
    item.elasticity= 0.5;
    [_animator addBehavior:item];
}

// 检测到碰撞后进行的处理
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id)item
withBoundaryIdentifier:(id)identifier atPoint:(CGPoint)p{

    if( !_firstContact) {
        _firstContact = YES;

        // 吸附,小球吸附在盒子里
        self.attachment = [[UIAttachmentBehavior alloc] initWithItem:_ball attachedToItem:_box];
        [self.animator addBehavior:_attachment];

        // 推
        UIPushBehavior * push
= [[UIPushBehavior alloc] initWithItems:@[_box]mode:UIPushBehaviorModeInstantaneous];
        CGVector pushDir = {0.5, -0.5};
        push.pushDirection =
pushDir;
        push.magnitude = 5.0;
        [_animator addBehavior:push];
    }
}
@end

Demo在这里

效果:


UIKit力学(Dynamics)_第3张图片

在SB为view添加一个点击手势


/**
 *  盒子停在界面中,当触摸屏幕,盒子会先加速后减速到触摸点
 */
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *box; // 盒子

@property (nonatomic, retain) UIDynamicAnimator * animator;
@property (nonatomic, retain) UISnapBehavior * snap; // 甩
@end

@implementation ViewController

- (void)viewDidLoad {
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
- (IBAction)handleSnapGesture:(UITapGestureRecognizer *)sender {
    // 移除甩行为
    [_animator removeBehavior:_snap];
    
    // 添加甩行为
    CGPoint point = [sender locationInView:self.view]; // 得到触摸的点
    self.snap = [[UISnapBehavior alloc] initWithItem:_box snapToPoint:point];
    [_animator addBehavior:_snap];
}

Demo在这里

你可能感兴趣的:(UIKit力学(Dynamics))