touchesMoved 在 iPhone 6 秒及以后的单次点击中被调用
touchesMoved being called on single Tap with iPhone 6s and onwards
我有一个自定义 UIButton 并且我在自定义按钮中实现了触摸委托 class。
在 iPhone 6 Plus 之前一切正常。它上面的所有设备,如 iPhone 6s 和 7 都会产生问题。
当我单击一个按钮时,touchesBegan
按预期被调用,但 touchesMoved
也被调用,这在我的代码中产生了问题。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
_firstTouch = [[touches anyObject]locationInView:self];
_isTapped = YES;
[self.nextResponder.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
_isTapped = NO;
[self.nextResponder.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (_isTapped){
//Functionality
}
为什么在这些设备上调用 touchesMoved
,我该如何解决?
可能是高分辨率的屏幕对任何运动都更敏感。当您敲击时,您实际上可能只是在滚动手指,使其看起来像是一个小动作。
两种可能的解决方案。
- 检查触摸在您的
touchesMoved:
方法中移动了多远。如果这是一个非常小的举动,为了您的 _isTapped
检查目的而忽略它。
- 不要覆盖
touches...
方法,而是使用 UITapGestureRecognizer
。让它完成确定什么是水龙头什么不是水龙头的所有艰苦工作。它将使您的代码更简单。
我有一个自定义 UIButton 并且我在自定义按钮中实现了触摸委托 class。
在 iPhone 6 Plus 之前一切正常。它上面的所有设备,如 iPhone 6s 和 7 都会产生问题。
当我单击一个按钮时,touchesBegan
按预期被调用,但 touchesMoved
也被调用,这在我的代码中产生了问题。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
_firstTouch = [[touches anyObject]locationInView:self];
_isTapped = YES;
[self.nextResponder.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
_isTapped = NO;
[self.nextResponder.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (_isTapped){
//Functionality
}
为什么在这些设备上调用 touchesMoved
,我该如何解决?
可能是高分辨率的屏幕对任何运动都更敏感。当您敲击时,您实际上可能只是在滚动手指,使其看起来像是一个小动作。
两种可能的解决方案。
- 检查触摸在您的
touchesMoved:
方法中移动了多远。如果这是一个非常小的举动,为了您的_isTapped
检查目的而忽略它。 - 不要覆盖
touches...
方法,而是使用UITapGestureRecognizer
。让它完成确定什么是水龙头什么不是水龙头的所有艰苦工作。它将使您的代码更简单。