0

In my swift ios app. I use below code to create a circle shape UIView on my screen. Then I add a UILongPressGestureRecognizer to it, keeping the view itself as well as the gestureRecognizer in their respective arrays. Every time user touch and hold onto this view, the code runs again and creates another view and add another UILongPressGestureRecognizer to it.

    let x = CGFloat(arc4random_uniform(UInt32(sW-w)))
    let y = CGFloat(arc4random_uniform(UInt32(sH-h-10)))+15

    circle.append(UIButton())
    touch.append(UILongPressGestureRecognizer())
    let l = circle.count-1

    circle[l].frame = CGRect(x:x, y:y, width:70, height:70)

    circle[l].backgroundColor = UIColor.redColor()
    circle[l].layer.cornerRadius = w/2
    circle[l].clipsToBounds = true

    touch[l].addTarget(self, action: "touched:")
    touch[l].minimumPressDuration = 0
    touch[l].numberOfTouchesRequired = 1
    touch[l].numberOfTapsRequired = 0
    touch[l].allowableMovement = 0
    touch[l].delegate = self

    circle[l].addGestureRecognizer(touch[l])
    self.view.addSubview(circle[l])

Now when I run the app, I can tap and hold on to the circle view and its state changes to .Began and it also fires .Changed and .Ended statuses up to 5 views. BUT when the 6th view is added, the gesture recognizer does not work on it.

There is nothing in Apple documentation about any maximum number of gestureRecognizers that would work simultaneously. What else could be causing this behaviour?

Kashif
  • 4,642
  • 7
  • 44
  • 97
  • Just wanted to update everyone interested in this question that after lots of research, as noted [here](http://stackoverflow.com/questions/3149868/determining-the-maximum-number-of-simultaneous-touches-possible-on-an-ios-device) I found that iPhone is limited to 5 simultaneous touches. – Kashif Mar 18 '15 at 18:10

1 Answers1

0

It's a bad approach to have more than 1 long press recognizer working simultaneously. All recognizers are reacting by chain after previous one in chain fails, if you not specifying by their delegate that they should recognize touches simultaneously.

But also you can implement a delegate callback for all your recognizers:

- (BOOL)gestureRecognizer:shouldRecieveTouch:

and check if touch fits to the corresponding recognizer. If yes - you can disable all other recognizers in this delegate callback, and after suitable recognizer handles the touch, then re-enable them again.

Eugene Dudnyk
  • 5,553
  • 1
  • 23
  • 48