如何解决UITapGestureRecognizer-使它在触地而不是触地工作吗?
创建您的自定义TouchDownGestureRecognizer子类,并在touches中实现手势。
TouchDownGestureRecognizer.h
#import <UIKit/UIKit.h>
@interface TouchDownGestureRecognizer : UIGestureRecognizer
@end
TouchDownGestureRecognizer.m
#import "TouchDownGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation TouchDownGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.state == UIGestureRecognizerStatePossible) {
self.state = UIGestureRecognizerStateRecognized;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.state = UIGestureRecognizerStateFailed;
}
@end
实施:
#import "TouchDownGestureRecognizer.h"
TouchDownGestureRecognizer *touchDown = [[TouchDownGestureRecognizer alloc] initWithTarget:self action:@selector(handletouchDown:)];
[yourView addGestureRecognizer:touchDown];
-(void)handletouchDown:(TouchDownGestureRecognizer *)touchDown{
NSLog(@"Down");
}
迅捷的实现:
import UIKit
import UIKit.UIGestureRecognizerSubclass
class TouchDownGestureRecognizer: UIGestureRecognizer
{
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)
{
if self.state == .Possible
{
self.state = .Recognized
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent)
{
self.state = .Failed
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent)
{
self.state = .Failed
}
}
这是2017年要粘贴的Swift语法:
import UIKit.UIGestureRecognizerSubclass
class SingletouchDownGestureRecognizer: UIGestureRecognizer {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
if self.state == .possible {
self.state = .recognized
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
self.state = .Failed
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
self.state = .Failed
}
}
请注意,这是的替代品UITap
。所以在像这样的代码中
func add(tap v:UIView, _ action:Selector) {
let t = UITapGestureRecognizer(target: self, action: action)
v.addGestureRecognizer(t)
}
您可以安全地交换到…。
func add(hairtriggerTap v:UIView, _ action:Selector) {
let t = SingletouchDownGestureRecognizer(target: self, action: action)
v.addGestureRecognizer(t)
}
测试表明它不会被多次调用。它可以作为替代品。您可以在两个通话之间进行交换。
解决方法
我使用tap事件的时间非常敏感,因此我很好奇是否有可能在用户简单按下时激活UITapGestureRecognizer,而不是要求他们同时按下吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。