简体   繁体   中英

Using iOS pan gesture to highlight buttons in grid

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!

UIButton inherits from UIControl which has a number of events

Have you tried that? You can add a listener to those events, either through nib/storyboard or through code (look under discussion to see how to do this)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM