[英]How to Implement Touch Up Inside in touchesBegan, touchesEnded
我想知道是否有人知道如何在用户按下时实现“内部touchesEnded
”响应然后在touchesBegan
, touchesEnded
方法中提起他们的手指。 我知道这可以通过UITapGestureRecognizer
来完成,但实际上我正在努力使它只能在快速点击时使用(使用UITapGestureRecognizer
,如果你长时间握住手指,然后抬起,它仍然执行)。 有谁知道如何实现这个?
使用UILongPressGesturizer
实际上是一个更好的解决方案来模仿UIButton
所有功能( touchUpInside
, touchUpOutside
, touchDown
等):
- (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];
}
}
}
}
您可以通过创建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])
{
}
}
我不确定它何时被添加,但属性isTouchInside
是任何UIControl
派生对象(例如UIButton
)的isTouchInside
。
override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
super.endTracking(touch, with: event)
if isTouchInside {
// Do the thing you want to do
}
}
您可以创建一些BOOL
变量,然后在-touchesBegan
检查触摸了哪个视图或任何您需要的内容,并将此BOOL
变量设置为YES
。 之后在-touchesEnded
检查此变量是否为YES
并且您的视图或您需要的任何内容都被触及,这将是您的-touchUpInside
。 当然,将BOOL
变量设置为NO
。
您可以添加UTapGestureRecognizer
和UILongPressGestureRecognizer
并使用[tap requiresGestureRecognizerToFail:longPress];
添加依赖项[tap requiresGestureRecognizerToFail:longPress];
(点击并长按是添加识别器的对象)。
这样,如果长按则不会检测到敲击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.