概述:UISwipeGestureRecognizer滑动手势,继承于UIGestureRecognizer类。
手势的配置
/*滑动的方向*/
@property(nonatomic) UISwipeGestureRecognizerDirection direction;
/*滑动的方向按位枚举*/
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
/*默认值,向右滑动*/
UISwipeGestureRecognizerDirectionRight = 1 << 0,
/*向左滑动*/
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
/*向上滑动*/
UISwipeGestureRecognizerDirectionUp = 1 << 2,
/*向下滑动*/
UISwipeGestureRecognizerDirectionDown = 1 << 3
};
/*滑动的手指数*/
@property(nonatomic) NSUInteger numberOfTouchesRequired __TVOS_PROHIBITED;
案例:
UISwipeGestureRecognizer* swipeDirection = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDirectionMethod:)];
/*设置滑动方向*/
swipeDirection.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeDirection];
/*方法实现*/
- (void) swipeDirectionMethod:(UISwipeGestureRecognizer*) swipeDirection
{
if(swipeDirection.direction == UISwipeGestureRecognizerDirectionRight)
NSLog(@"向右滑动,%lu", swipeDirection.direction);
}
注意:如果要实现多个方向滑动,必须定义新的UISwipeGestureRecognizer对象,而不是用运算符“|”来表示,例如:
/*错误*/
UISwipeGestureRecognizer* swipeDirection = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDirectionMethod:)];
swipeDirection.direction = UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeDirection];
/*正确*/
UISwipeGestureRecognizer* swipeDirection_Right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDirectionRightMethod:)];
UISwipeGestureRecognizer* swipeDirection_Left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDirectionLeftMethod:)];
swipeDirection_Right.direction = UISwipeGestureRecognizerDirectionRight;
swipeDirection_Left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer: swipeDirection_Right];
[self.view addGestureRecognizer: swipeDirection_Left];
/*方法实现*/
- (void) swipeDirectionRightMethod:(UISwipeGestureRecognizer *)swipe {
NSLog(@"向右滑动");
}
- (void) swipeDirectionLeftMethod:(UISwipeGestureRecognizer *)swipe {
NSLog(@"向左滑动");
}
注:了解UIGestureRecognizer类请跳转https://www.jianshu.com/p/e206dc86f89a