gpt4 book ai didi

iOS 自定义手势识别器仅在第一次工作

转载 作者:行者123 更新时间:2023-11-28 22:52:44 37 4
gpt4 key购买 nike

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

我正在使用以下代码:

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

知道我做错了什么吗?

谢谢

最佳答案

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

我知道有两种方法可以让它发挥作用:

  1. 您可以在 touchesEnded 方法中自行将状态属性设置为 UIGestureRecognizerStatePossible。但这似乎是错误的。
  2. 您可以使用 UIGestureRecognizer 的状态属性代替您自己的状态。 IE。到处使用 super.state 而不是 self.state。

关于iOS 自定义手势识别器仅在第一次工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11540659/

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