0

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: {})
                }
            }
Dakata
  • 1,227
  • 2
  • 14
  • 33
  • What happens when you swipe? Did you debug? Is `handleSwipes` even called? – matt Dec 04 '16 at 22:10
  • This is the error I get: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Audio.ViewController handleSwipes]: unrecognized selector sent to instance 0x100d0be50' – Dakata Dec 04 '16 at 22:13
  • Yes, I thought that might be what was happening! :) – matt Dec 04 '16 at 22:16

1 Answers1

1

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Actually it needs to be `handleSwipes(sender:)`, not just `handleSwipes`. – rmaddy Dec 04 '16 at 22:17
  • @rmaddy No it doesn't. – matt Dec 04 '16 at 22:18
  • Why not? The actual method takes a parameter so the selector needs to include the parameter. – rmaddy Dec 04 '16 at 22:19
  • 1
    @rmaddy No it doesn't. The bare name is sufficient provided there is no ambiguity. And there is no ambiguity. See my discussion here: http://stackoverflow.com/a/35658335/341994 where I explain the syntax for you. – matt Dec 04 '16 at 22:21
  • Thanks. That's a subtlety of Swift I didn't know. – rmaddy Dec 04 '16 at 22:24
  • @rmaddy I know you didn't. :) I much prefer the "bare name" syntax; it saves a mess of typing. – matt Dec 04 '16 at 22:24
  • Thanks also, @matt. I've wondered why both ways of #selector works - seems odd given how much work has gone into making Swift "safe". Given you an up vote for that. –  Dec 04 '16 at 23:37