开发者

Subclassing UIPanGestureRecognizer to wait for Condition before recognition

开发者 https://www.devze.com 2023-03-30 17:44 出处:网络
I\'m trying to link two gestures one after another. UILongPressGestureRecognizer, then UIPanGestureRecognizer.

I'm trying to link two gestures one after another. UILongPressGestureRecognizer, then UIPanGestureRecognizer.

I want to detect the Long Press, then allow the Pan gesture to be recognized.

I've Subclassed UIPanGestureRecognizer and Added an panEnabled Bool iVar. In the initWith Frame I've set panEnabled to NO.

In Touches Moved I check to see if it is enabled, and then call Super touchesMoved if it is.

In my LongPress Gesture Handler, I loop though the View's Gestures till I find my Subclassed Gesture and then setPanEnabled to YES.

It seems like it is working, though its like the original pan gesture recognizer is not functioning properly and not setting the Proper states. I know if you Subclass the UIGestureRecognizer, you need to maintain the state yourself, but I would think that if you are subclassing UIPanGestureRecognizer, and for all the touches methods calling the super, that it would be setting the state in there.

Here is my subclass .h File

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

@interface IoUISEListPanGestureRecognizer : UIPanGestureRecogn开发者_开发百科izer {
    int IoUISEdebug;
    BOOL panEnabled;    
}
- (id)initWithTarget:(id)target action:(SEL)action;
@property(nonatomic, assign) int IoUISEdebug;
@property(nonatomic, assign) BOOL panEnabled;

@end

here is the subclass .m File

#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


If you make a BOOL called canPan and include the following delegate methods you can have both a standard UILongPressGestureRecognizer and UIPanGestureRecognizer attached to the same view. On the selector that gets called when the long press gesture is recognized - change canPan to YES. You might want to disable the long press once it has been recognised and re-enable it when the pan finishes. - Don't forget to assign the delegate properties on the gesture recognisers.

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

    return YES;
}

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号