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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容