【问题标题】:How to access camera with swiping gesture?如何通过滑动手势访问相机?
【发布时间】:2017-04-19 06:09:24
【问题描述】:

我在我的 ios 应用程序中添加了滑动手势,但不知何故之后我无法访问相机。我是 swift 的初学者,如果你能建议我如何解决它,我会很高兴。 到目前为止我用过:

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

【问题讨论】:

  • 滑动时会发生什么?你调试了吗? handleSwipes 甚至被调用了吗?
  • 这是我得到的错误:由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[Audio.ViewController handleSwipes]: unrecognized selector sent to instance 0x100d0be50'
  • 是的,我认为这可能是正在发生的事情! :)

标签: swift camera gesture


【解决方案1】:

你在哪里:

action: Selector(("handleSwipes"))

这样写:

action: #selector(handleSwipes)

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

【讨论】:

  • 其实应该是handleSwipes(sender:),而不仅仅是handleSwipes
  • @rmaddy 不,它没有。
  • 为什么不呢?实际方法需要一个参数,因此选择器需要包含该参数。
  • @rmaddy 不,它没有。只要没有歧义,裸名就足够了。并且没有歧义。请在此处查看我的讨论:stackoverflow.com/a/35658335/341994 我为您解释语法。
  • 谢谢。这是我不知道的 Swift 的微妙之处。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多