概述:UIPanGestureRecognizer拖动手势,继承于UIGestureRecognizer类。
手势的配置
/*设置拖动最小手指数,默认为1*/
@property (nonatomic) NSUInteger minimumNumberOfTouches __TVOS_PROHIBITED; /*设置拖动最多手指数,默认为UINT_MAX,无限大*/
@property (nonatomic) NSUInteger maximumNumberOfTouches __TVOS_PROHIBITED;
手势的位置和速度
/*获取拖动的位置*/
- (CGPoint)translationInView:(nullable UIView *)view;
/*设置当前拖动的位置*/
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;
/*设置拖动的速度,单位:点/秒*/
- (CGPoint)velocityInView:(nullable UIView *)view;
案例
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)];
[panGestureRecognizer setDelegate:self];
[self.image_view addGestureRecognizer:panGestureRecognizer];
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer{
/*获取状态*/
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
/*获取拖动的位置*/
CGPoint translation = [recognizer translationInView:recognizer.view];
/*每次都以传入的translation为起始参照*/
[recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
/*设置当前拖动的位置*/
[recognizer setTranslation:CGPointZero inView:recognizer.view];
if (self.image_view.frame.size.width <= self.view.bounds.size.width) {
self.image_view.frame = self.view.bounds;
}
}
}
注:了解UIGestureRecognizer类请跳转https://www.jianshu.com/p/e206dc86f89a