3

I have an ImageView added to UIView. Tapping on the image take to Segmented Control which is also part of same UIView. I am trying to add swipe gestures to this segmented control.

Tried following.

override func viewDidLoad() {
    super.viewDidLoad()

    let rightSwipe = UISwipeGestureRecognizer(target: SegmentCotroller, action: Selector("swiped:"))
    rightSwipe.direction = .Right
    self.SegmentCotroller.addGestureRecognizer(rightSwipe)
}

func swiped(sender:UIGestureRecognizer){
    print("Swiped.....!")
}

Code never reaches to swiped method when swiping right.

Any help is appreciated! Thanks

Alexey Pichukov
  • 3,377
  • 2
  • 19
  • 22
user360
  • 350
  • 3
  • 9

1 Answers1

3

You need to se the swipe gestures target to self

let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swiped:"))
rightSwipe.direction = .Right
self.segmentController.addGestureRecognizer(rightSwipe)

The target sets where the action will be executed so you want it pointing at wherever you have the action implemented.

Moriya
  • 7,750
  • 3
  • 35
  • 53
  • Thanks for your response. I did try that earlier but didn't work. – user360 Nov 15 '15 at 09:20
  • Ok. Then you have something else wrong in your controller. I tried it and it works just fine. How did you set up your segmented control? is it an outlet or do you create it in code? – Moriya Nov 15 '15 at 09:21
  • Can you explain your usage a bit more and perhaps post a screenshot? Your question is a bit hard to undersand. – Moriya Nov 15 '15 at 09:23
  • I do have outlet for segmentedController @IBOutlet weak var SegmentCotroller: UISegmentedControl! – user360 Nov 15 '15 at 09:24
  • Well.. I tried your swipeGestureRecognizer code on a programatically created segmentedController and apart from the target there was nothing wrong with the code. So it's hard to help you without any more info about the rest of the code or some screenshot. Sorry – Moriya Nov 15 '15 at 09:31
  • Thanks Moriya for your help. The code is very huge so hard to send the screenshot but I did realized my mistake. I was trying to swipe the collection view cell which loads on tapping segmented control instead of segmented control itself. It is working now when I swipe on the segmented control. – user360 Nov 15 '15 at 09:45