#import "SnapViewController.h"
@interface SnapViewController ()
{
// 在定义成员变量时,默认都是strong的,如果需要定义为weak类型,需要使用__weak的描述符
__weak IBOutlet UIImageView *_boxImageView;
UIDynamicAnimator *_animator;
UISnapBehavior *_snap;
}
@end
@implementation SnapViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1. 实例化Animator
// _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_animator=[[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
#pragma mark - 点按手势
- (IBAction)tapGesture:(UITapGestureRecognizer *)sender
{
// // 将之前定义的吸附行为删除
// [_animator removeBehavior:_snap];
//
// // 获取手势的点击位置
// CGPoint location = [sender locationInView:self.view];
//
// // 实例化吸附行为
// _snap = [[UISnapBehavior alloc] initWithItem:_boxImageView snapToPoint:location];
//
// // 指定随机的振幅
// CGFloat num = arc4random_uniform(10) / 10.0f;
// // 默认振幅是0.5
// _snap.damping = num;
//
// // 将吸附行为添加到animator
// [_animator addBehavior:_snap];
// _animator
[_animator removeBehavior:_snap];
CGPoint location=[sender locationInView:self.view];
_snap=[[UISnapBehavior alloc] initWithItem:_boxImageView snapToPoint:location];
CGFloat num=arc4random_uniform(10) /10.0f;
_snap.damping=num;
[_animator addBehavior:_snap];
}
@end
@interface SnapViewController ()
{
// 在定义成员变量时,默认都是strong的,如果需要定义为weak类型,需要使用__weak的描述符
__weak IBOutlet UIImageView *_boxImageView;
UIDynamicAnimator *_animator;
UISnapBehavior *_snap;
}
@end
@implementation SnapViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1. 实例化Animator
// _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_animator=[[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
#pragma mark - 点按手势
- (IBAction)tapGesture:(UITapGestureRecognizer *)sender
{
// // 将之前定义的吸附行为删除
// [_animator removeBehavior:_snap];
//
// // 获取手势的点击位置
// CGPoint location = [sender locationInView:self.view];
//
// // 实例化吸附行为
// _snap = [[UISnapBehavior alloc] initWithItem:_boxImageView snapToPoint:location];
//
// // 指定随机的振幅
// CGFloat num = arc4random_uniform(10) / 10.0f;
// // 默认振幅是0.5
// _snap.damping = num;
//
// // 将吸附行为添加到animator
// [_animator addBehavior:_snap];
// _animator
[_animator removeBehavior:_snap];
CGPoint location=[sender locationInView:self.view];
_snap=[[UISnapBehavior alloc] initWithItem:_boxImageView snapToPoint:location];
CGFloat num=arc4random_uniform(10) /10.0f;
_snap.damping=num;
[_animator addBehavior:_snap];
}
@end