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<UITouch *> *)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