0

我正在尝试使用文档中的复选标记手势识别器示例,但它仅在我第一次做出手势时才有效。识别后,又不行了。

我正在使用以下代码:

CheckmarkGestureRecognizer.h

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

@interface Checkmark2GestureRecognizer : UIGestureRecognizer {
    BOOL strokeUp ;
}


- (void)reset;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

@property (nonatomic,readwrite) UIGestureRecognizerState state;
@property (nonatomic,assign) CGPoint midPoint;
@end

CheckmarkGestureRecognizer.m

#import "Checkmark2GestureRecognizer.h"


@implementation Checkmark2GestureRecognizer

@synthesize state;
@synthesize midPoint;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
            NSLog(@"began");
    strokeUp = NO;
    if ([touches count] != 1) {
        self.state = UIGestureRecognizerStateFailed;
        return;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    if (self.state == UIGestureRecognizerStateFailed) return;
    CGPoint nowPoint = [[touches anyObject] locationInView:self.view];
    CGPoint prevPoint = [[touches anyObject] previousLocationInView:self.view];
    if (!strokeUp) {
        // on downstroke, both x and y increase in positive direction
        if (nowPoint.x >= prevPoint.x && nowPoint.y >= prevPoint.y) {
            self.midPoint = nowPoint;
            // upstroke has increasing x value but decreasing y value
        } else if (nowPoint.x >= prevPoint.x && nowPoint.y <= prevPoint.y) {
            strokeUp = YES;
        } else {
            self.state = UIGestureRecognizerStateFailed;
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    if ((self.state == UIGestureRecognizerStatePossible) && strokeUp) {
        self.state = UIGestureRecognizerStateRecognized;
            NSLog(@"Ended");
    }

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    self.midPoint = CGPointZero;
    strokeUp = NO;
    self.state = UIGestureRecognizerStateFailed;
    NSLog(@"cancelled");
}

- (void)reset {
    [super reset];
    NSLog(@"reset");
    self.midPoint = CGPointZero;
    strokeUp = NO;
}

@end

知道我做错了什么吗?

谢谢

4

3 回答 3

2

我自己的手势识别器也有类似的问题。似乎 UIGestureRecognizer 没有调用你的类的“reset”方法,也没有将“state”属性设置为 UIGestureRecognizerStatePossible。

我知道两种使它起作用的方法:

  1. 您可以在 touchesEnded 方法中自行将 state 属性设置为 UIGestureRecognizerStatePossible。但这似乎是错误的。
  2. 您可以使用 UIGestureRecognizer 的 state 属性而不是您自己的 state。即在任何地方都使用 super.state 而不是 self.state。
于 2012-08-07T13:56:07.990 回答
0

查看此代码。它对我很好。 https://github.com/shahidnasrullah/CustomZGestureIOS.git

于 2013-06-11T05:42:11.463 回答
0

尝试@synthesize state;在您的实现中删除该行。这已经在超类中完成了。

您也不必将所有定义复制UIGestureRecognizerSubclass.h到您的头文件中。最好是也删除。

于 2013-09-21T15:36:15.663 回答