Search code examples
swiftcameragesture

How to access camera with swiping gesture?


I have added a swipe gesture in my ios application, but somehow I can't access the camera afterward. I am beginner in swift and I would be very glad if you can advice me how to fix it. So far I have used:

  import UIKit

 class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate{

 let imagePicker: UIImagePickerController! = UIImagePickerController()
 let reachability = Reachability()!

override func viewDidLoad() {

    super.viewDidLoad()

    imagePicker.delegate = self

    self.view.backgroundColor = UIColor.flatBlackColorDark()

    let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("handleSwipes")))

    upSwipe.direction = .up

    view.addGestureRecognizer(upSwipe)

}

and for the function:

func handleSwipes(sender:UISwipeGestureRecognizer) {

        if (sender.direction == .up){

            if ( UIImagePickerController.isSourceTypeAvailable(.camera)){
                if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
                    imagePicker.allowsEditing = false
                    imagePicker.sourceType = .camera
                    imagePicker.cameraCaptureMode = .photo
                    present(imagePicker,animated: true, completion: {})
                }
            }

Solution

  • Where you have:

    action: Selector(("handleSwipes"))
    

    put this:

    action: #selector(handleSwipes)
    

    The reason is that "handleSwipes" is not the selector for your handleSwipes function, and you do not know how to form the selector correctly. But the compiler does know, and using #selector syntax tells it to do so.