3

I am setting up a UISwipeGestureRecognizer for an UIImageView on my ViewController.swift file but my manual swipe on my iPhone only works 1 out of 10 times, if that. Do I only need one GestureRecognizer for the directions of left/right? Or did I code it correctly by setting up two GestureRecognizers?

Any idea on what the issue could be?

 import MobileCoreServices

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate {

@IBOutlet weak var carImage: UIImageView!

var Swiper: UISwipeGestureRecognizer!
var Swiper2: UISwipeGestureRecognizer! 

func swipeAction2(sender: UISwipeGestureRecognizer) {
    print("Swiper2")
    let location = sender.locationInView(self.view)
    if self.carImage.pointInside(location, withEvent: nil) {
        if sender.direction == UISwipeGestureRecognizerDirection.Right {
            let animation : CATransition = CATransition()
            animation.type = kCATransitionPush
            animation.subtype = kCATransitionFromLeft
            animation.duration = NSTimeInterval(1.0)
            animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            animation.fillMode = kCAFillModeForwards
            self.carImage.layer.addAnimation(animation, forKey: nil)
        }
    }

}

func swipeAction(sender: UISwipeGestureRecognizer) {
    print("Swiper")
    let location = sender.locationInView(self.view)
    if self.carImage.pointInside(location, withEvent: nil){
        if sender.direction == UISwipeGestureRecognizerDirection.Left {
            let animation : CATransition = CATransition()
            animation.type = kCATransitionPush
            animation.subtype = kCATransitionFromRight
            animation.duration = NSTimeInterval(1.0)
            animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            animation.fillMode = kCAFillModeForwards
            self.carImage.layer.addAnimation(animation, forKey: nil)

        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    carImage.userInteractionEnabled = true
    self.Swiper = UISwipeGestureRecognizer(target: self, action: "swipeAction:")
    self.Swiper.numberOfTouchesRequired = 1
    self.Swiper.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(self.Swiper)
    self.Swiper2 = UISwipeGestureRecognizer(target: self, action: "swipeAction2:")
    self.Swiper2.numberOfTouchesRequired = 1
    self.Swiper2.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(self.Swiper2)

}
pmoney13
  • 1,075
  • 3
  • 11
  • 19
  • Are there other gesture recognizers set up in your view controller? – sunny Jul 06 '15 at 18:34
  • No, only these two swipe gesture recognizers are set up in my view controller. – pmoney13 Jul 06 '15 at 18:44
  • 1
    Does it print swiper and swiper2 only one of the 10 times too? I just pasted your code without the animation code and it works fine. – FireDragonMule Jul 06 '15 at 19:44
  • Really? What do you mean without the animation code? And yes the Swiper and Swiper2 print in the logs 100% of the swipes. – pmoney13 Jul 06 '15 at 19:52
  • Oh, I just meant the transition code after your print. If it prints, it's not the gesture recognizer. It's most likely going to be this line here: self.carImage.pointInside(location, withEvent: nil). Can you confirm that's always true when you need it to be? – FireDragonMule Jul 06 '15 at 20:00
  • Yes I can confirm that it is always true when I need it to be. – pmoney13 Jul 06 '15 at 20:14

0 Answers0