我想对 UIPanGestureRecognizer
进行子类化,以便在我的子类中只有在平移过程中达到某个距离阈值后,状态才会更改为 UIGestureRecognizerStateBegan
。这意味着经过一定程度的平移后,我希望平移手势的状态变为 UIGestureRecognizerStateBegan。
我尝试插入 touchesBegan
并手动将状态设置为失败,然后在 touchesMoved
中将其更新为开始,但我只想将其更改为开始一次(即第一次达到阈值时,后续交互将是 UIGestureRecognizerStateChanged
这可能吗?
最佳答案
这是一个似乎有效的子类。它会延迟“开始”状态,直到达到所需的距离。
DelayedPanGestureRecognizer.h:
#import <UIKit/UIKit.h>
@interface DelayedPanGestureRecognizer : UIPanGestureRecognizer
@property (nonatomic, assign) CGFloat delay;
@end
DelayedPanGestureRecognizer.m:
#import "DelayedPanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation DelayedPanGestureRecognizer
- (void)setState:(UIGestureRecognizerState)state {
if (state == UIGestureRecognizerStateBegan) {
CGPoint trans = [self translationInView:self.view];
if (trans.x * trans.x + trans.y * trans.y > self.delay * self.delay) {
[super setState:state];
}
} else {
[super setState:state];
}
}
@end
用法:
DelayedPanGestureRecognizer *pan = [[DelayedPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)];
pan.delay = 6;
[someView addGestureRecognizer:pan];
以及 Action :
- (void)panned:(DelayedPanGestureRecognizer *)gesture {
NSLog(@"State: %d", (int)gesture.state);
NSLog(@"%@", NSStringFromCGPoint([gesture translationInView:gesture.view]));
if (gesture.state == UIGestureStateBegan) {
// do something
} else if (gesture.state == UIGestureStateChanged) {
// do other things
}
}
关于ios - 是否可以将 UIPanGestureRecognizer 子类化以使其在达到某个距离阈值后出现开始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50752248/