gpt4 book ai didi

ios - 使用 UIGestureRecogniser 子类化时未调用 Touches Ended

转载 作者:行者123 更新时间:2023-11-28 18:55:00 25 4
gpt4 key购买 nike

我正在尝试为我的 View 创建自定义手势识别器。我正在关注这里提到的这个答案:但由于某种原因,没有调用已触摸的 Ended 和已拍摄的触摸。只有触摸开始才会被调用。

子类:

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

@implementation TapGesture

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.state == UIGestureRecognizerStatePossible) {
self.state = UIGestureRecognizerStateRecognized;
}
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

}

@end

我正在按如下方式初始化 TapGesture:

TapGesture *tapGesture=[[TapGesture alloc]initWithTarget:self action:@selector(incrementValue)];
tapGesture.delaysTouchesEnded=NO;
tapGesture.cancelsTouchesInView=NO;
[_workButton addGestureRecognizer:tapGesture]; //_workButton is a UIView

我在 View 中没有任何其他手势识别器。如果我在 UIView 中使用相同的方法,所有这些方法都会按预期调用。

为什么在 UIGestureRecogniser 类中覆盖时没有调用 touchesEnded/touchesMoved?

最佳答案

当子类化 UIGestureRecognizer 时,您必须使其表现得像一个连续 手势并由您自己处理它的状态机(即,手动设置它的状态 ).

来自 UIGestureRecognizer 上的 iOS 开发者库文档:

Subclasses must set the state property to the appropriate value when they transition between states.

See here for more info (向下滚动到子类注释)

  • 注意:要使 state 可读/可写,而不是只读,您应该使用 UIGestureRecognizerSubclass.h,如文档中所述:

    The state property is declared in UIGestureRecognizer.h as being read-only. This property declaration is intended for clients of gesture recognizers. Subclasses of UIGestureRecognizer must import UIGestureRecognizerSubclass.h. This header file contains a redeclaration of state that makes it read-write.

关于ios - 使用 UIGestureRecogniser 子类化时未调用 Touches Ended,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34934555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com