使用iOS平移手势突出显示网格中的按钮 [英] Using iOS pan gesture to highlight buttons in grid

查看:158
本文介绍了使用iOS平移手势突出显示网格中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有一个网格的按钮,并已附加一个平移手势识别器的视图。我可以跟踪事件,并获得手指的位置,因为它移动,但似乎没有相当于一个mouseEnter消息用于获取有关的信息或控制属性(如突出显示)的其他按钮我过去了。



我错过了什么?我们如何完成,说,突出显示在用户的手指下面的按钮,因为他们在他们?



谢谢。

解决方案

你是对的,没有这样的事件。此外,UIButton事件也不会帮助你,因为那些需要实际启动手势内。您可以做的,是获取您当前拖动的点的位置:

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

,当你有点,你可以迭代所有的按钮,并检查点是否在按钮内:

  let buttons = [UIButton] 
let lastActiveButton = UIButton?

...

//遍历所有按钮
按钮中的按钮{

//检查按钮区域对你的触摸
如果CGRectContainsPoint(button.frame,touchPoint){

//你在按钮的触摸区域内
//这里,你可以执行一些动作如果你想,存储信息
//关于按钮,所以你不要多次等等。你的调用:)
self.lastActiveButton = button
}
}

这样你就可以检测到,然后你进入和退出,做任何你想要的事件。希望它帮助!


So far I have a grid of buttons and have attached a pan gesture recognizer to the view. I can track the events and get the location of the finger as it moves but there doesn't seem to be the equivalent of a "mouseEnter" message to use to get info about or control properties (such as the highlighting) of the other buttons I pass over.

Am I missing something? How can I accomplish, say, highlighting the buttons under the users' fingers as they pan over them? Does cocoa touch support this or must something else be done?

Thanks.

解决方案

You are right, there is no such event. Also UIButton events won't help you either, because those require to actually start gesture inside. What you can do instead is to get location of the point you are currently dragging:

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

And now, when you have the point, you can iterate on all the buttons you have and check if the point is inside the 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
   }
}

This way you can detect then you go in and out and do whatever you want with events. Hope it helps!

这篇关于使用iOS平移手势突出显示网格中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆