gpt4 book ai didi

objective-c - 子类化 UIPanGestureRecognizer 以在识别之前等待条件

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:09 26 4
gpt4 key购买 nike

我试图将两个手势一个接一个地联系起来。 UILongPressGestureRecognizer,然后是 UIPanGestureRecognizer。

我想检测长按,然后允许识别平移手势。

我对 UIPanGestureRecognizer 进行了子类化并添加了一个 panEnabled Bool iVar。在 initWith Frame 中,我将 panEnabled 设置为 NO。

在 Touches Moved 中,我检查它是否已启用,如果已启用,则调用 Super touchesMoved。

在我的 LongPress 手势处理程序中,我遍历 View 的手势,直到找到我的子类手势,然后将 PanEnabled 设置为 YES。

它似乎正在工作,尽管它就像原始的平移手势识别器无法正常工作并且没有设置正确的状态。我知道如果您将 UIGestureRecognizer 子类化,您需要自己维护状态,但我认为如果您将 UIPanGestureRecognizer 子类化,并且对于调用 super 的所有触摸方法,它将在那里设置状态。

这是我的子类.h文件

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

@interface IoUISEListPanGestureRecognizer : UIPanGestureRecognizer {
int IoUISEdebug;
BOOL panEnabled;
}
- (id)initWithTarget:(id)target action:(SEL)action;
@property(nonatomic, assign) int IoUISEdebug;
@property(nonatomic, assign) BOOL panEnabled;

@end

这里是子类的.m文件

#import "IoUISEListPanGestureRecognizer.h"

@implementation IoUISEListPanGestureRecognizer
@synthesize IoUISEdebug;
@synthesize panEnabled;

- (id)initWithTarget:(id)target action:(SEL)action {
[super initWithTarget:target action:action];
panEnabled = NO;
return self;
}

- (void)ignoreTouch:(UITouch*)touch forEvent:(UIEvent*)event {
[super ignoreTouch:touch forEvent:event];
}

-(void)reset {
[super reset];
panEnabled = NO;
}

- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer {
return [super canPreventGestureRecognizer:preventedGestureRecognizer];
}

- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer{
return [super canBePreventedByGestureRecognizer:preventingGestureRecognizer];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if (panEnabled) {
[super touchesMoved:touches withEvent:event];
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesCancelled:touches withEvent:event];
}
@end

最佳答案

如果创建一个名为 canPan 的 BOOL 并包含以下委托(delegate)方法,则可以将标准 UILongPressGestureRecognizer 和 UIPanGestureRecognizer 附加到同一 View 。在识别长按手势时调用的选择器上 - 将 canPan 更改为 YES。您可能希望在识别出长按后禁用它,并在平移完成后重新启用它。 - 不要忘记在手势识别器上分配委托(delegate)属性。

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if (!canPan && [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return NO;
}

return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

关于objective-c - 子类化 UIPanGestureRecognizer 以在识别之前等待条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7179690/

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