如何一键轻扫多个button

我有5个button 每个允许touchUpInside行动和touchDragOutside行动…以及通过tapGestureRecognizers doubleTap行动和longPress行动。

我也想允许用户从任何UIButton和任何额外的UIButton滑动触摸开始滑动 ,这些button(包括第一次触摸)执行他们的@IBAction func Swipe

所以像这样连续滑动 在这里输入图像说明 将执行UIButtons 1,2,3,4和5的@IBAction func swipe

你可以尝试这样的事情:

 // Create an array to hold the buttons you've swiped over var buttonArray:NSMutableArray! override func viewDidLoad() { super.viewDidLoad() // Make your view's UIPanGestureRecognizer call panGestureMethod: // (don't use a UISwipeGestureRecognizer since it's a discrete gesture) panGesture.addTarget(self, action: "panGestureMethod:") } func panGestureMethod(gesture:UIPanGestureRecognizer) { // Initialize and empty array to hold the buttons at the // start of the gesture if gesture.state == UIGestureRecognizerState.Began { buttonArray = NSMutableArray() } // Get the gesture's point location within its view // (This answer assumes the gesture and the buttons are // within the same view, ex. the gesture is attached to // the view controller's superview and the buttons are within // that same superview.) let pointInView = gesture.locationInView(gesture.view) // For each button, if the gesture is within the button and // the button hasn't yet been added to the array, add it to the // array. (This example uses 4 buttons instead of 9 for simplicity's // sake if !buttonArray.containsObject(button1) && CGRectContainsPoint(button1.frame, pointInView){ buttonArray.addObject(button1) } else if !buttonArray.containsObject(button2) && CGRectContainsPoint(button2.frame, pointInView){ buttonArray.addObject(button2) } else if !buttonArray.containsObject(button3) && CGRectContainsPoint(button3.frame, pointInView){ buttonArray.addObject(button3) } else if !buttonArray.containsObject(button4) && CGRectContainsPoint(button4.frame, pointInView){ buttonArray.addObject(button4) } // Once the gesture ends, trigger the buttons within the // array using whatever control event would otherwise trigger // the button's method. if gesture.state == UIGestureRecognizerState.Ended && buttonArray.count > 0 { for button in buttonArray { (button as UIButton).sendActionsForControlEvents(UIControlEvents.TouchUpInside) } } } 

(编辑:这里有几个答案,我已经写在过去解释我的意思是UISwipeGestureRecognizer是一个离散的手势: stackoverflow.com/a/27072281/2274694,stackoverflow.com/a/25253902/2274694 )