iOS - 左右滑动手势,获取手指位置

2022-01-06  本文已影响0人  HanZhiZzzzz

方法一:只能识别,不能获取位置

@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;

@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
@synthesize leftSwipeGestureRecognizer,rightSwipeGestureRecognizer;
  1. 在视图控制器里加载这两个手势
   self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

   self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

   self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;

   self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;

   [self.viewaddGestureRecognizer:self.leftSwipeGestureRecognizer]; 

   [self.viewaddGestureRecognizer:self.rightSwipeGestureRecognizer];
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender

{

    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {

    //添加要响应的方法

            }

        if (sender.direction == UISwipeGestureRecognizerDirectionRight) {

    //添加要响应的方法

           }

}

摘自:https://www.cnblogs.com/someonelikeyou/p/3512268.html

方法二: 会与系统的cell左滑手势冲突,若添加在cell上,cell左滑手势不会执行

    // 手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.contentView addGestureRecognizer:pan];
    - (void)pan:(UIPanGestureRecognizer *)pan
{
    //获取偏移量
    // 返回的是相对于最原始的手指的偏移量
    CGPoint transP = [pan translationInView:self.contentView];
    DLog(@"transP -- %lf",transP.x);

}

方法三: 简单方便

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];

    CGPoint touchPoint = [touch locationInView:self.contentView];

    DLog(@"BeganPoint -- %lf",touchPoint.x);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];

    CGPoint touchPoint = [touch locationInView:self.contentView];

    DLog(@"touchPoint -- %lf",touchPoint.x);
}
上一篇下一篇

猜你喜欢

热点阅读