【发布时间】:2014-11-13 08:56:39
【问题描述】:
我对 touchesBegan 处理程序的调用超出必要有一个奇怪的问题。 如果我快速点击 UIView (UIButton) 2 次,touchesBegan 会被调用 3 次。
我通过简单的时间测量解决了这个问题,但我仍然感兴趣这种行为的原因是什么?
这是代码(已经添加了时间检查):
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if let t:UITouch = touches.anyObject() as? UITouch
{
if !CGRectContainsPoint(CGRectMake(0, 0, self.frame.width, self.frame.height), t.locationInView(self))
{
touchesCancelled(touches, withEvent: event)
}
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
ForCancelTouch = false
setupButtonGUI(true)
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
if !ForCancelTouch
{
if abs(LastValidTouchesBeganDate.timeIntervalSinceNow) > DelayBetweenFastTapping
{
NSNotificationCenter.defaultCenter().postNotificationName(SBDCNotificationNameActionSTBMakeOneCommand, object: self, userInfo: ["tag":self.tag])
LastValidTouchesBeganDate = NSDate()
}
}
setupButtonGUI(false)
}
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
ForCancelTouch = true
setupButtonGUI(false)
}
【问题讨论】:
-
也许按钮会产生另一个这样的事件?您可以尝试使用
UIView进行重构。从概念上讲,我不希望直接处理控件的触摸。 -
也许还有一个 UIScrollView?