【发布时间】:2017-02-11 00:25:34
【问题描述】:
我正在开发一个 iOS 应用程序,我希望在用户的手指滑入特定区域时获得动作效果。我首先认为拖入触摸的 UIButton 会起作用,但在“拖动”之前仍然需要触摸。那么有没有办法做到这一点?非常感谢!
【问题讨论】:
-
也许 UIButtons touch down 会在拖动时触发? (与内部的正常修饰相对)
我正在开发一个 iOS 应用程序,我希望在用户的手指滑入特定区域时获得动作效果。我首先认为拖入触摸的 UIButton 会起作用,但在“拖动”之前仍然需要触摸。那么有没有办法做到这一点?非常感谢!
【问题讨论】:
您可以在触摸开始时使用UITouch。将您的特定区域视图作为UIView IBOutlet。让我们称之为particularAreaView
可能是这样的:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let touch = touches.allObjects[0] as UITouch //get the latest touch
let touchLocation = touch.locationInView(self.view)
let particularAreaViewFrame = self.view.convertRect(particularAreaView.frame, fromView: self.view)
//Check if touch began is in your particularAreaViewFrame
if CGRectContainsPoint(particularAreaViewFrame, touchLocation) {
tiggerYourActionHere()
}
}
如果您需要它来考虑正在移动到该区域的触摸,您还必须覆盖 touchesMoved: withEvent:。然后也许将上面的内容重构为一个在两个地方都可以使用的函数。
【讨论】: