{
__weak IBOutlet UIImageView *_boxImageView;
UIDynamicAnimator *_animator;
UIPushBehavior *_push;
// 是否第一次点按的标记
BOOL _isFirstTap;
// 第一次点按的点坐标
CGPoint _firstPoint;
}
@end
@implementation PushViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[_boxImageView setTransform:CGAffineTransformMakeRotation(45)];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(110, 300, 20, 20)];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIDynamicItemBehavior *item = [[UIDynamicItemBehavior alloc] initWithItems:@[view]];
item.elasticity = 0.8f;
[_animator addBehavior:item];
// 实例化一个推的行为
_push = [[UIPushBehavior alloc] initWithItems:@[_boxImageView] mode:UIPushBehaviorModeInstantaneous];
[_animator addBehavior:_push];
// 实例化碰撞行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[_boxImageView, view]];
collision.translatesReferenceBoundsIntoBoundary= YES;
[_animator addBehavior:collision];
}
#pragma mark 点按手势
- (IBAction)tapGesture:(UITapGestureRecognizer *)sender
{
_isFirstTap = !_isFirstTap;
if (_isFirstTap) {
_firstPoint = [sender locationInView:self.view];
return;
}
// 取出第二个点的位置,计算两点间的距离和角度
CGPoint p = [sender locationInView:self.view];
// powf 是计算float数值的多少次方
CGFloat xOffset = p.x - _firstPoint.x;
CGFloat yOffset = p.y - _firstPoint.y;
// 计算距离
CGFloat dis = sqrtf(powf(xOffset, 2.0) + powf(yOffset, 2.0f));
// 计算角度
CGFloat angle = atan2(yOffset, xOffset);
_push.angle = angle;
_push.magnitude = dis / 30.0f;
// 在使用单次推行为时,需要使用active激活该次推操作
_push.active = YES;
}
@end
__weak IBOutlet UIImageView *_boxImageView;
UIDynamicAnimator *_animator;
UIPushBehavior *_push;
// 是否第一次点按的标记
BOOL _isFirstTap;
// 第一次点按的点坐标
CGPoint _firstPoint;
}
@end
@implementation PushViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[_boxImageView setTransform:CGAffineTransformMakeRotation(45)];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(110, 300, 20, 20)];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIDynamicItemBehavior *item = [[UIDynamicItemBehavior alloc] initWithItems:@[view]];
item.elasticity = 0.8f;
[_animator addBehavior:item];
// 实例化一个推的行为
_push = [[UIPushBehavior alloc] initWithItems:@[_boxImageView] mode:UIPushBehaviorModeInstantaneous];
[_animator addBehavior:_push];
// 实例化碰撞行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[_boxImageView, view]];
collision.translatesReferenceBoundsIntoBoundary= YES;
[_animator addBehavior:collision];
}
#pragma mark 点按手势
- (IBAction)tapGesture:(UITapGestureRecognizer *)sender
{
_isFirstTap = !_isFirstTap;
if (_isFirstTap) {
_firstPoint = [sender locationInView:self.view];
return;
}
// 取出第二个点的位置,计算两点间的距离和角度
CGPoint p = [sender locationInView:self.view];
// powf 是计算float数值的多少次方
CGFloat xOffset = p.x - _firstPoint.x;
CGFloat yOffset = p.y - _firstPoint.y;
// 计算距离
CGFloat dis = sqrtf(powf(xOffset, 2.0) + powf(yOffset, 2.0f));
// 计算角度
CGFloat angle = atan2(yOffset, xOffset);
_push.angle = angle;
_push.magnitude = dis / 30.0f;
// 在使用单次推行为时,需要使用active激活该次推操作
_push.active = YES;
}
@end