2016-12-04 44 views
0

我在我的ios应用程序中添加了滑动手势,但不知何故,我无法访问摄像头。我是初学者,如果你能告诉我如何解决这个问题,我会很高兴。 到目前为止,我已经使用:如何使用滑动手势访问摄像头?

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) 

} 

和功能:

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: {}) 
       } 
      } 
+0

当你刷卡时会发生什么?你调试了吗? 'handleSwipes'甚至被称为? – matt

+0

这是我得到的错误:终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [Audio.ViewController handleSwipes]:无法识别的选择器发送到实例0x100d0be50' – Dakata

+0

是的,我认为这可能是发生了什么! :) – matt

回答

1

如果您有:

action: Selector(("handleSwipes")) 

把这个:

action: #selector(handleSwipes) 

的原因是那"handleSwipes"不是您的handleSwipes函数的选择器,和不知道如何正确地形成选择器。但编译器确实知道,并且使用#selector语法告诉它这样做。

+0

实际上它需要'handleSwipes(sender:)',而不仅仅是'handleSwipes'。 – rmaddy

+0

@rmaddy不,它不。 – matt

+0

为什么不呢?实际的方法需要一个参数,所以选择器需要包含参数。 – rmaddy