关于使用UIPanGestureRecognizer手势touchesBegan不调用的问题

在iOS开发中,遇到使用UIPanGestureRecognizer时,要精确获取触摸开始点的挑战。常规情况下,手势需要滑动后才触发,导致起始点不准确。通过重写手势的`touchesBegan`方法并调用父类实现,可以在开始触摸时立即获取点,解决了轻扫时不触发的问题。参考Stack Overflow解决方案进行实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近使用UIPanGestureRecognizer手势时遇到一个问题,就是想获取起始的触摸点,但UIPanGestureRecognizer手势需要滑动一点距离时,才会触发,那样获取的起始点不太准确。然后就想到了

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

这个方法,但这个方法还是有个缺点,就是开始触摸时,需要稍微停顿一下才会触发,如果你轻扫的话,就不会调用,显然效果不太理想。

那我们可以继承UIPanGestureRecognizer,然后重写UIPanGestureRecognizer的

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

方法,然后调用父类的touchesBegan方法就可以了,代码片段如下:

#import "ImmediatePanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@implementation ImmediatePanGestureRecognizer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    self.state = UIGestureRecognizerStateBegan;
}

@end

你也可以从上面的重写方法中,直接得到point

  UITouch *touch=[touches anyObject];
  CGPoint  startPoint =[touch locationInView:touch.view];

参考自:
http://stackoverflow.com/questions/17621402/uipangesturerecognizer-do-something-immediately-when-touched

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值