【问题标题】:Subclassing UIPanGestureRecognizer to wait for Condition before recognition子类化 UIPanGestureRecognizer 以在识别前等待 Condition
【发布时间】:2011-11-03 00:40:27
【问题描述】:

我正在尝试将两个手势一个接一个地关联起来。 UILongPressGestureRecognizer,然后是 UIPanGestureRecognizer。

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

我已经继承了 UIPanGestureRecognizer 并添加了一个 panEnabled Bool iVar。在 initWith Frame 中,我将 panEnabled 设置为 NO。

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

在我的 LongPress 手势处理程序中,我循环查看视图的手势,直到找到我的子类手势,然后将 setPanEnabled 设置为 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

【问题讨论】:

  • 你能分享这个手势子类化的实际代码吗?在我看来,我正在努力实现这一点,但两个猜测器都不能正常工作。每次 Longpress 工作正常。

标签: objective-c ios uigesturerecognizer uitouch subclassing


【解决方案1】:

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

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

    return YES;
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2021-12-23
    • 2021-11-05
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    相关资源
    最近更新 更多