iOS Tap手势状态开始没有命中
Posted
技术标签:
【中文标题】iOS Tap手势状态开始没有命中【英文标题】:iOS Tap gesture state begin doesn't hit 【发布时间】:2013-11-19 10:14:43 【问题描述】:我使用点击手势识别器制作了一个可点击的视图,效果很好。但是我想在触摸发生时突出显示视图,然后在触摸结束时将其删除。
我试过这个:
- (IBAction)refresh:(UITapGestureRecognizer *)sender
if(self.currentStatus == NODATA)
if(sender.state == UIGestureRecognizerStateBegan)
NSLog(@"Began!");
[self.dashboardViewController.calendarContainer state:UIViewContainerStatusSELECTED];
if (sender.state == UIGestureRecognizerStateEnded)
NSLog(@"%@", @"Ended");
[self.dashboardViewController.calendarContainer state:UIViewContainerStatusNORMAL];
[self setState:REFRESHING data:nil];
“Ended does”的 NSLog 被显示,但 begin 没有显示,因此它永远不会进入选中状态。这是为什么呢?
【问题讨论】:
【参考方案1】:UITapGestureRecognizer
永远不会进入UIGestureRecognizerStateBegan
状态。对于从UIGestureRecognizerStatePossible
到UIGestureRecognizerStateBegan
的识别器,只会产生连续 手势(例如滑动或捏合)。 离散的手势,例如点击,将其识别器直接放入UIGestureRecognizerStateRecognized
,即单击一下,直接放入UIGestureRecognizerStateEnded
。
也就是说,也许您正在寻找UILongPressGestureRecognizer
,它是一个连续识别器,将输入UIGestureRecognizerStateBegan
,让您能够辨别触摸的开始和结束?
【讨论】:
【参考方案2】:可能为时已晚。但是,如果您严格地想使用手势识别器,这也会对您有所帮助。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(refresh:)];
longPress.minimumPressDuration = 0.0;
- (IBAction)refresh:(UILongPressGestureRecognizer *)sender
if(self.currentStatus == NODATA)
if(sender.state == UIGestureRecognizerStateBegan)
NSLog(@"Began!");
[self.dashboardViewController.calendarContainer state:UIViewContainerStatusSELECTED];
if (sender.state == UIGestureRecognizerStateEnded)
NSLog(@"%@", @"Ended");
[self.dashboardViewController.calendarContainer state:UIViewContainerStatusNORMAL];
[self setState:REFRESHING data:nil];
【讨论】:
【参考方案3】:您还可以使用touchesBegan:withEvent:
和touchesEnded:withEvent:
方法。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
NSSet *t = [event touchesForView:_myView];
if([t count] > 0)
// Do something
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
NSSet *t = [event touchesForView:_myView];
if([t count] > 0)
// Do something
【讨论】:
谢谢你我修好了【参考方案4】:斯威夫特 4.2 ?
let recognizer = UILongPressGestureRecognizer(target: self, action: Selector("touched:"))
recognizer.delegate = self
recognizer.minimumPressDuration = 0.0
addGestureRecognizer(recognizer)
userInteractionEnabled = true
/**
* Gesture handler
*/
@objc func touched(sender: UILongPressGestureRecognizer)
if sender.state == .began
/*onPressed*/
else if sender.state == .ended
/*onReleased*/
【讨论】:
【参考方案5】:相当老的问题,但仍然。希望它对某人有所帮助。如果作者的问题是正确的,那么想法是检测水龙头是否已经开始识别并执行操作。就像您不希望目标仅在用户松开手指时触发,而是在他第一次触摸时触发。
一个简单的方法是为UITapGestureRecognizer
进行扩展,如下所示:
fileprivate class ModTapGestureRecognizer: UITapGestureRecognizer
var onTouchesBegan: (() -> Void)?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent)
onTouchesBegan?()
super.touchesBegan(touches, with: event)
稍后在您的代码中,您可以像这样使用它:
let tapRecognizer = ModTapGestureRecognizer()
tapRecognizer.addTarget(self, action: #selector(didTapped))
tapRecognizer.onTouchesBegan =
print("Yep, it works")
yourView.addGestureRecognizer(tapRecognizer)
你太棒了!
【讨论】:
以上是关于iOS Tap手势状态开始没有命中的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 watchkit 应用程序没有在“开始”状态调用我的平移手势识别器处理代码?