iOS DynamicAnimator(物理仿真)

物理仿真
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIView *blueView;

/** 物理仿真器*/
@property (strong, nonatomic) UIDynamicAnimator *animator;

/** 捕捉行为*/
@property (strong, nonatomic)  UISnapBehavior *snap;

@end

@implementation ViewController

- (UIDynamicAnimator *)animator
{
    if (_animator == nil) {
        _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    }
    return _animator;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.redView.transform = CGAffineTransformRotate(self.redView.transform, 40);
    
}

#pragma mark 点击屏幕开始重力和碰撞仿真行为
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (self.snap == nil) {
        // iOS8以前, 是无法更改point, 如果要更改, 需要移除并重新添加
        //0. 获取点击的点
        CGPoint point = [[touches anyObject] locationInView:self.view];
        
        //1. 添加snap之前, 需要移除之前的行为
        
        // 如果需要更改snapPoint, 此方法就不需要写了
        //[self.animator removeAllBehaviors];
        
        //2. 创建物理仿真行为(顺便设置物理仿真元素)
        // Point自行设置即可
        self.snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:point];
        
        //2.1 设置 振幅 减震效果  0最大, 1最小
        self.snap.damping = 0.5;
        
        //3. 将仿真行为添加到仿真器中
        [self.animator addBehavior:self.snap];
    }
    
    //2.2 设置位置
    static int i = 1;
    self.snap.snapPoint = CGPointMake(200, 50 * i);
    i++;

}


//点击屏幕开始重力+碰撞仿真行为
- (void)gravityAndCollision
{
    //1. 创建物理仿真器(顺便设置仿真范围)
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    
    //2. 创建物理仿真行为(顺便设置物理仿真元素)
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
    
    //2.1 向量, 重力方向
    gravity.gravityDirection = CGVectorMake(3, 1);
    
    //2.2 角度 默认就是90°, M_PI_2
    gravity.angle = M_PI_2;
    
    //2.3 重力加速度 越小越慢
    gravity.magnitude = 100;
    
    UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView, self.blueView]];
    
    // 设置参考引用的视图的边框为自己的边框
    collision.translatesReferenceBoundsIntoBoundary = YES;
    
    // 设置碰撞的边界
    //    CGPoint fromPoint = CGPointMake(0, [UIScreen mainScreen].bounds.size.height * 0.2);
    //    CGPoint toPoint = CGPointMake(375, [UIScreen mainScreen].bounds.size.height * 0.8);
    //    [collision addBoundaryWithIdentifier:@"collision" fromPoint:fromPoint toPoint:toPoint];
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 375, 375)];
    [collision addBoundaryWithIdentifier:@"collision" forPath:bezierPath];
    
    //3. 将仿真行为添加到仿真器中
    [self.animator addBehavior:gravity];
    [self.animator addBehavior:collision];
}


//点击屏幕开始重力仿真行为
- (void)gravity
{
    //1. 创建物理仿真器(顺便设置仿真范围)
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    
    //2. 创建物理仿真行为(顺便设置物理仿真元素)
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
    
    //3. 将仿真行为添加到仿真器中
    [self.animator addBehavior:gravity];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值