2

I am trying to add tap recognizers to my iCarousel project. In order to accomplish this, I have written this:

func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
    let rView = UIView(frame: someOtherView.frame)
    let imageView = UIImageView(frame: someOtherView.frame)
    imageView.frame.size.height -= 30
    imageView.frame.size.width -= 50
    imageView.image = (images[index])
    imageView.contentMode = .ScaleAspectFit
    imageView.backgroundColor = UIColor.whiteColor()
    imageView.userInteractionEnabled = true
    rView.addSubview(imageView)
    if let gestures = rView.gestureRecognizers {
        for gesture in gestures {
            rView.removeGestureRecognizer(gesture)
        }
    }
    let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.moveCarousel(_:)))
    rView.addGestureRecognizer(tap)
    print(rView.gestureRecognizers)
    setImageToCenter(imageView)
    return rView
}

This works fine in portrait mode. However, when I rotate to landscape, it does not work. Nothing happens when I tap the view. Here it is what I call when the screen is rotated:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
    slider.setWidth()
    trademarkCarousel.reloadData()
}

How can I run the TapGestureRecognizer when my device is in landscape? Thanks.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61

0 Answers0