Day.03.12 UISnapBehavior 吸附行为

20W以下的项目用不到,20W以上的项目用一点
ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
    UIDynamicAnimator *_animator;
    UIImageView *_imgV;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建ImageView
    _imgV = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    
    [_imgV setImage:[UIImage imageNamed:@"Box1.png"]];
    
    [self.view addSubview:_imgV];
    
    //1⃣️ 仿真器初始化
    _animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    
    /*———————————UISnapBehavior———————————————————————————————————————————————————————————————————-*/
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    
    CGPoint point = [touch locationInView:self.view];
    
    //point 是我手指点击屏幕的坐标点

    /*——————————————————————————————————————————————————————————————————————————————-*/
    
    //移除所有行为
    [_animator removeAllBehaviors];
    
    //移除某个行为
//    [_animator removeBehavior:(nonnull UIDynamicBehavior *)];
    
    //2⃣️吸附行为
    UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:_imgV snapToPoint:point];
    
    //振幅 (强 0.0 -- 1.0 弱)
    snap.damping = 0.0;
    
    //3⃣️仿真器添加行为
    [_animator addBehavior:snap];
    
}
@end

Day.03.12 UISnapBehavior 吸附行为_第1张图片
屏幕快照 2016-03-12 下午4.52.23.png

你可能感兴趣的:(Day.03.12 UISnapBehavior 吸附行为)