iOS开发中使用UIDynamic来捕捉动画组件的重力行为

2020-01-14 18:20:13丽君

    }
    return _animator;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //获取一个触摸点
    UITouch *touch=[touches anyObject];
    CGPoint point=[touch locationInView:touch.view];
    
    //1.创建捕捉行为
    //需要传入两个参数:一个物理仿真元素,一个捕捉点
    UISnapBehavior *snap=[[UISnapBehavior alloc]initWithItem:self.blueView snapToPoint:point];
    //设置防震系数(0~1,数值越大,震动的幅度越小)
    snap.damping=arc4random_uniform(10)/10.0;
    
    //2.执行捕捉行为
    //注意:这个控件只能用在一个仿真行为上,如果要拥有持续的仿真行为,那么需要把之前的所有仿真行为删除
    //删除之前的所有仿真行为
    [self.animator removeAllBehaviors];
    [self.animator addBehavior:snap];
}

@end

 

注:相关教程知识阅读请移步到IOS开发频道。