2

I have 5 buttons; each allows touchUpInside-action and touchDragOutside-action...as well as doubleTap-action and longPress-action via tapGestureRecognizers.

I would also like to allow user to start a swipe from any UIButton and Any addition UIButton that the swipe touches, these buttons (including first touch) perform their @IBAction func Swipe.

so a continuously swipe like so enter image description here would perform @IBAction func swipe for UIButtons 1,2,3,4 and 5.

Chameleon
  • 1,608
  • 3
  • 21
  • 39

1 Answers1

1

You can try something like this:

// 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)
        }
    }
}

(Edit: Here are a few answers I've written in the past explaining what I meant by UISwipeGestureRecognizer being a discrete gesture: stackoverflow.com/a/27072281/2274694, stackoverflow.com/a/25253902/2274694)

Community
  • 1
  • 1
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • That is so awesome‼‼ Ty so much. However I am confused on one concept here. `if !buttonArray.containsObject(button1)` is the "!" mean "does not(contain)"? because the way i read this otherwise its saying if `button1` is in `buttonArray`, add `button1` to `buttonArray`.×Comments may only be edited for 5 minutes×Comments may only be edited for 5 minutes×Comments may only be edited for 5 minutes – Chameleon Mar 20 '15 at 22:28
  • @Dustin Yes, it means does not contain. – Lyndsey Scott Mar 20 '15 at 22:29
  • @Dustin No worries. :) – Lyndsey Scott Mar 20 '15 at 22:29
  • @Lyndsey_Scott while i have you on the line, can I ask you what it means that UISwipeGestureRecognizer is a **"discrete"** gesture. I did notice early that i couldn't use it to create a segue like other gestures. – Chameleon Mar 20 '15 at 22:36
  • @Dustin Here are a couple answers I've written to explain: http://stackoverflow.com/a/27072281/2274694, http://stackoverflow.com/a/25253902/2274694 – Lyndsey Scott Mar 20 '15 at 22:39