0

In my ViewController I have three views. I want to add UIPanGestureRecognizer in this.

Below is my code...

var circlePanGesture  = UIPanGestureRecognizer()
var sqaurePanGesture  = UIPanGestureRecognizer()
var trianglePanGesture  = UIPanGestureRecognizer()

var destination = [UIView]()

@IBOutlet weak var circle_source: UIView!
@IBOutlet weak var square_source: UIView!
@IBOutlet weak var triangle_source: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    panGestureConfiguration()
}

func UIConfiguration() {
    circle_source.layer.cornerRadius = circle_source.frame.size.width/2
    circle_source.clipsToBounds = true        
}

func panGestureConfiguration() {
    circlePanGesture.addTarget(self, action: #selector(draggedSourceView(_:)))
    circle_source.addGestureRecognizer(circlePanGesture)

    sqaurePanGesture.addTarget(self, action: #selector(draggedSourceView(_:)))
    square_source.addGestureRecognizer(sqaurePanGesture)

    trianglePanGesture.addTarget(self, action: #selector(draggedSourceView(_:)))
    triangle_source.addGestureRecognizer(trianglePanGesture)

}

In this ViewController's panGestureConfiguration function I want to use only one UIPanGestureRecognizer object instead of three. So how to do it?

Please give me any solution for this.

Vikas
  • 1,548
  • 2
  • 12
  • 25
  • 1
    No thats not possible for each view you need to have separate `UIPanGestureRecognizer` object – Nirav D Aug 06 '18 at 07:09
  • I want to pass two views to one UIPanGestureRecognizer object – Vikas Aug 06 '18 at 07:10
  • Thats not possible check this [SO question](https://stackoverflow.com/questions/40483099/uitapgesturerecognizer-working-on-uiimageview-but-not-on-uilabel/) for more detail – Nirav D Aug 06 '18 at 07:13
  • actually you don't pass VCs to 'UIPanGestureRecognizer' instead of that you should set 'gestureRecognizer' to VCs, then for each view you should do that. – Arash Etemad Aug 06 '18 at 07:45
  • I am not undestanding...please tell me in detail. – Vikas Aug 06 '18 at 07:57
  • @VikasDhasal You can't set one gesture to multiple views, if you try to set one gesture to multiple views then the view to which you'll set the gesture in last will have effect of it and gestures from other views will get removed. – Kamaldeep singh Bhatia Aug 06 '18 at 08:31
  • Can I directly initialize views in addGestureRecognizer() – Vikas Aug 06 '18 at 08:32
  • see this answer https://stackoverflow.com/a/5567684/5622566 for more detail – Kamaldeep singh Bhatia Aug 06 '18 at 08:32
  • Possible duplicate of [Can you attach a UIGestureRecognizer to multiple views?](https://stackoverflow.com/questions/4747238/can-you-attach-a-uigesturerecognizer-to-multiple-views) – Kamaldeep singh Bhatia Aug 06 '18 at 08:34
  • And here you can find a workaround if you wan this to reduce number of lines in code https://stackoverflow.com/a/40405568/5622566 – Kamaldeep singh Bhatia Aug 06 '18 at 08:34

0 Answers0