使用选择器操作不调用函数

时间:2016-07-12 12:44:02

标签: swift

我试图在外部UIViewController的类中处理轻击手势但我的函数从未被调用过。这是我的 代码:

import SceneKit
import UIKit

class SceneManager
{
private let assetFolder: String
private let mainCamera: SCNNode
private let view: SCNView
private let scene: SCNScene

init(view: SCNView, assetFolder: String, sceneFile: String, mainCameraName: String, backgroundColor: UIColor) {
    self.assetFolder = assetFolder
    self.scene = SCNScene(named: (self.assetFolder + "/scene/" + sceneFile))!
    self.mainCamera = self.scene.rootNode.childNodeWithName(mainCameraName, recursively: true)!
    self.view = view
    self.view.backgroundColor = backgroundColor
    self.view.allowsCameraControl = false
    self.view.pointOfView = self.mainCamera
    self.view.scene = self.scene

    //PROBLEM BELOW
    let gesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
    self.view.addGestureRecognizer(gesture)
}

@objc func handleTap(sender: UITapGestureRecognizer) {
    print("hello")
}
}

这是我的ViewController类: 导入UIKit 导入QuartzCore 导入SceneKit

class ViewController: UIViewController
{
override func viewDidLoad()
{
    super.viewDidLoad()
    let view = self.view as! SCNView
    view.showsStatistics = true

    _ = SceneManager(view: view, assetFolder: "art.scnassets", sceneFile: "EURO_COPTER.dae", mainCameraName: "camera", backgroundColor: UIColor.blackColor())
}
}

如果有人有想法。提前谢谢。

2 个答案:

答案 0 :(得分:0)

尝试这种方式: -

声明类时,在类子类之后添加UIGestureRecognizerDelegate。这就是我的情况:

let gesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
self.view.userInteractionEnabled = true;
gesture.delegate = self
self.view.addGestureRecognizer(gesture)

答案 1 :(得分:0)

尝试以下可能有用的代码

let gesture = UITapGestureRecognizer(target: self, action: #selector(SceneManager.handleTap(_:)))