UIPushBehavior的简单使用


#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是继承自UIDynamicBehaviorUIDynamicBehavior是继承自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,必须要设置magnitudeangle两个属性

    _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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值