IOS UISwipeGestureRecognizer works intermittently - Swift / Xcode

I'm setting up a UISwipeGestureRecognizer for a UIImageView in my ViewController.swift file, but my manual only works 1 out of 10 times on my iPhone, if possible. Do I only need one GestureRecognizer for left / right direction? Or did I code it correctly by setting two GestureRecognizers?

Any idea on what might be the problem?

 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)

}

      

+3


source to share





All Articles