仿真动画(UIDynamicAnimator,UIGravityBehavior,UICollisionBehavior)

仿真动画 

/**
 *  仿真器,也是仿真行为的执行者
 */
@property (nonatomic, strong) UIDynamicAnimator *animator;

/**
 *  重力行为
 */
@property (nonatomic, strong) UIGravityBehavior *gravity;

/**
 *  碰撞行为
 */
@property (nonatomic, strong) UICollisionBehavior *collision;
/**
 *  仿真器,也是仿真行为的执行者
 */
- (UIDynamicAnimator *)animator
{
    if (!_animator)
    {
        _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    }
    
    return _animator;
}

/**
 *  重力行为
 */
- (UIGravityBehavior *)gravity
{
    if (!_gravity)
    {
        _gravity = [[UIGravityBehavior alloc] init];
    }
    
    return _gravity;
}

/**
 *  碰撞行为
 */
- (UICollisionBehavior *)collision
{
    if (!_collision)
    {
        _collision = [[UICollisionBehavior alloc] init];
        //不会超过编辑
        _collision.translatesReferenceBoundsIntoBoundary = YES;
    }
    
    return _collision;
}

/**
 *  创建一个视图
 *
 *  @param frame           frame description
 *  @param backgroundColor backgroundColor description
 *
 *  @return return value description
 */
- (UIView *)createViewWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor
{
    UIView *v = [[UIView alloc] initWithFrame:frame];
    v.backgroundColor = backgroundColor;
    [self.view addSubview:v];
    return v;
}

- (void)createItem
{
    int width = 40;
    
    //视图
    UIView *view = [self createViewWithFrame:CGRectMake(arc4random_uniform(330), 0, width, width) backgroundColor:[UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]];
    //view.layer.cornerRadius = width/2;
    
    //添加仿真元素
    [self.gravity addItem:view];
    [self.collision addItem:view];
    
    
    //执行重力行为
    [self.animator addBehavior:self.gravity];
    [self.animator addBehavior:self.collision];
}

最后在viewdidload执行

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(createItem) userInfo:nil repeats:YES];

Demo地址:https://github.com/JnKindle/UIDynamicAnimator

欢迎大家访问我的GitHub

GitTub:https://github.com/JnKindle

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值