#import "PushView.h"
@interface PushView ()
@property (weak, nonatomic) UIImageView * imageView1;
@property (strong,nonatomic) UIDynamicAnimator * animator;
@property (strong,nonatomic) UIPushBehavior * push;
@property (assign,nonatomic) CGPoint beganPoint;
@property (assign,nonatomic) CGPoint endPoint;
@property (assign,nonatomic) CGPoint movePoint;
@end
@implementation PushView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [superinitWithFrame:frame]) {
self.backgroundColor = [UIColorwhiteColor];
[selfsetupViews];
}
return self;
}
- (void)setupViews{
UIImageView * imageView1 = [[UIImageViewalloc]init];
imageView1.backgroundColor = [UIColorredColor];
imageView1.frame = CGRectMake(50, 100, 100, 100);
imageView1.layer.masksToBounds =YES;
imageView1.layer.cornerRadius = imageView1.frame.size.width/2;
imageView1.userInteractionEnabled =YES;
_imageView1 = imageView1;
[self addSubview:imageView1];
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)];
[selfaddGestureRecognizer:pan];
/**
UIPushBehavior是继承自UIDynamicBehavior,UIDynamicBehavior是继承自NSObject
这个行为若有UICollisionBehavior如下,则这个行为最终停留在屏幕边缘,也就是他的父视图的边缘
如果没有UICollisionBehavior这个行为的载体最终会消失 滑出父视图的边缘
*/
//UIPushBehaviorModeInstantaneous 力是瞬时的
_push = [[UIPushBehavioralloc] initWithItems:@[self.imageView1]mode:UIPushBehaviorModeInstantaneous];
//UIPushBehaviorModeContinuous 力是连续的
_push = [[UIPushBehavioralloc] initWithItems:@[self.imageView1]mode:UIPushBehaviorModeContinuous];
[self.animatoraddBehavior:_push];
//创建了之后默认是YES 用这个属性来决定这个行为是否执行
NSLog(@"active %d ",_push.active);
//加一个UICollisionBehavior就不会出现在屏幕外面
UICollisionBehavior * collision = [[UICollisionBehavioralloc]initWithItems:@[self.imageView1]];
collision.translatesReferenceBoundsIntoBoundary =YES;
[self.animatoraddBehavior:collision];
}
- (void)pan:(UIPanGestureRecognizer *)pan{
CGPoint location = [pan locationInView:self];
if (UIGestureRecognizerStateBegan == pan.state) {
_beganPoint = location;
} elseif (UIGestureRecognizerStateChanged == pan.state){
_movePoint = location;
[selfsetNeedsDisplay];
} elseif (UIGestureRecognizerStateEnded == pan.state){
_endPoint = location;
_push.active =YES;
CGPoint offset =CGPointMake(_beganPoint.x -_endPoint.x,self.beganPoint.y -_endPoint.y);
_push.active =YES;
// 角度 默认的是0如果要配置力矢量UIPushBehavior,必须要设置magnitude和angle两个属性
_push.angle = -atan2f(offset.y, -offset.x);
// 推动行为的力矢量的大小默认是nil 若为负则是力的反方向
_push.magnitude =sqrtf(offset.x * offset.x + offset.y * offset.y) /50;
//这个方法相当于设定angle和 magnitude
[_push setAngle:-atan2f(offset.y, -offset.x)magnitude:sqrtf(offset.x * offset.x + offset.y * offset.y) / 50];
[selfclearLine];
}
}
- (void)drawRect:(CGRect)rect{
//绘制拉伸线路
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, self.beganPoint.x,self.beganPoint.y);
CGContextAddLineToPoint(context,_movePoint.x,_movePoint.y);
CGContextSetLineWidth(context,5.0f);
CGFloat length[] = {15.0,5.0};
CGContextSetLineDash(context, 0.0, length, 2);
[[UIColorblueColor] setStroke];
CGContextDrawPath(context,kCGPathFillStroke);
}
- (void)clearLine{
_beganPoint = CGPointZero;
_movePoint =CGPointZero;
_endPoint =CGPointZero;
[selfsetNeedsDisplay];
}
- (UIDynamicAnimator *)animator{
if (_animator ==nil) {
_animator = [[UIDynamicAnimatoralloc]initWithReferenceView:self];
}
return_animator;
}
@end