使用iOS平移手势突出显示网格中的button

到目前为止,我有一个button的网格,并附加了一个平移手势识别器的视图。 我可以跟踪事件并获取手指移动的位置,但似乎并不等同于用于获取有关其他button的属性(如突出显示)的信息的“mouseEnter”消息我过去了。

我错过了什么吗? 我怎样才能完成,突出显示用户的手指下面的button,当他们平移他们? cocoa触摸是否支持这一点,还是必须做其他事情?

谢谢。

你是对的,没有这样的事件。 此外,UIButton事件也不会帮助你,因为那些需要真正开始手势。 你可以做的是获得你正在拖动的点的位置:

func panDetected(sender : MoreInformativeGestureRecognizer) { let touchPoint = sender.locationInView(self.view) } 

而现在,当你有这个观点时,你可以迭代你所有的button,并检查点是否在button内:

 let buttons = [UIButton] let lastActiveButton = UIButton? ... // Iterate through all the buttons for button in buttons { // Check area of the button against your touch if CGRectContainsPoint(button.frame, touchPoint) { // You are inside the touch area of the button // Here, you can for example perform some action if you want, store information // about the button so you don't do it multiple times etc.. your call :) self.lastActiveButton = button } } 

这样,你可以检测到,你进出,做任何你想要的事件。 希望能帮助到你!

UIButton从具有多个事件的 UIControlinheritance

你尝试过吗? 你可以添加一个监听器到这些事件,或者通过笔尖/故事板或者通过代码(看看正在讨论,看看如何做到这一点)