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:角阻力:物体旋转的时候,旋转方向的阻力

用代码说话

重力、碰撞、吸附、推

先看效果:


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


Paste_Image.png
/**
 *  盒子重力加速度下降
 *  盒子在碰到棍子后弹起翻转
 *  盒子碰到球吸附
 */

@interface ViewController () <UICollisionBehaviorDelegate>{
    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<UIDynamicItem>)item
withBoundaryIdentifier:(id<NSCopying>)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在这里

效果:


在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动力学最大的特点是将现实世界动力驱动的动画引入了UIKit,比如重力,铰链连接,碰撞,悬挂等效果,即将2...
    BarleyZ阅读 1,324评论 0 49
  • iOS 7增加了UIKit Dynamics库,其集成于UIKit框架中,将2D物理引擎引入了UIKit,提供了以...
    pro648阅读 2,859评论 2 14
  • 《围城》里比较意外的是方鸿渐与孙柔嘉的婚姻。 孙柔嘉爱方鸿渐吗?方是她改变命运最便捷的棋子,却未必是她掏心掏肺死去...
    江南玉笛阅读 573评论 5 5
  • 经过几日舟车劳顿,他们终于到了药王谷,此时的古月北看上去消瘦了很多,也许只有仇恨才足以支撑他快悲伤过度的躯体。 微...
    想最烈的酒阅读 325评论 0 2
  • 于涛和路梅十年没见了,他们是网恋的五年前。路梅深爱着于涛,于涛总是找理由不见路梅。最后路梅忍不住来到北京,在一个寒...
    臭余阅读 202评论 0 1