【发布时间】: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