如何实现touch在触摸开始,touchesEnded

我想知道如果有人知道如何实现“触摸里面”的反应,当用户按下,然后举起touchesBegantouchesEnded方法的手指。 我知道这可以用UITapGestureRecognizer完成,但实际上我正在试图使它只能快速点击(使用UITapGestureRecognizer ,如果你长时间保持你的手指,然后提起,它仍然执行)。 任何人都知道如何实现这个?

使用UILongPressGesturizer实际上是一个更好的解决scheme来模仿UIButton所有function( touchUpInsidetouchUpOutsidetouchDown等):

 - (void) longPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer { if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan || longPressGestureRecognizer.state == UIGestureRecognizerStateChanged) { CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self]; if (CGRectContainsPoint(self.bounds, touchedPoint)) { [self addHighlights]; } else { [self removeHighlights]; } } else if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded) { if (self.highlightView.superview) { [self removeHighlights]; } CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self]; if (CGRectContainsPoint(self.bounds, touchedPoint)) { if ([self.delegate respondsToSelector:@selector(buttonViewDidTouchUpInside:)]) { [self.delegate buttonViewDidTouchUpInside:self]; } } } } 

你可以通过创build一个UIView子类并在那里实现touchesBegan和touchesEnded。

不过,您也可以使用UILongPressGestureRecognizer并获得相同的结果。

我通过在touchesBegan中触发一个计时器来做到这一点。 如果这个计时器在touchesEnded被调用时仍在运行,那么执行你想要的任何代码。 这给了touchUpInside的效果。

  -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSTimer *tapTimer = [[NSTimer scheduledTimerWithTimeInterval:.15 invocation:nil repeats:NO] retain]; self.tapTimer = tapTimer; [tapTimer release]; } -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if ([self.tapTimer isValid]) { } } 

你可以创build一些BOOLvariables,然后在-touchesBegan检查什么视图或任何你需要被触摸,并设置这个BOOLvariables为YES 。 之后在-touchesEnded检查,如果这个variables是YES ,你的观点或任何你需要的东西被触动,那将是你的-touchUpInside 。 当然,设置BOOLvariables为NO后。

您可以添加一个UTapGestureRecognizer和一个UILongPressGestureRecognizer并使用[tap requiresGestureRecognizerToFail:longPress];添加依赖关系[tap requiresGestureRecognizerToFail:longPress]; (点击和长按是添加识别器的对象)。

这样,如果长时间按下,水龙头将不会被检测到。