如何访问Siri Remote播放/暂停按钮和覆盖菜单按钮

时间:2016-04-23 15:21:58

标签: swift tvos uitapgesturerecognizer siri-remote

如何使用Siri遥控器访问播放/暂停按钮并覆盖菜单按钮?我目前正在使用它,但它对我不起作用。当我使用这段代码时,我的程序崩溃,但只有当我按下暂停按钮时才调用它 编码器目前位于touchesBegan旁边的didMoveToView下面

let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)

3 个答案:

答案 0 :(得分:1)

您的问题是您正在调用一个名为handleTap:的函数,它接收一个参数,但您没有一个名为handleTap:的函数。这就是action在这一行中代表的内容:

let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))

将您的func tapped()更改为:

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Ended {
        print("Menu button released")
    }
}

答案 1 :(得分:1)

我将以下内容用于Swift 4(根据问题:@selector() in Swift?

func doRequest(_ completion: @escaping (Result<TResponseData>) -> Void) {

    let completeURL = // ...
    let parameters =  // ...
    let encoding =    // ...
    let headers =     // ...

    Alamofire
        .request(
            completeURL, method: .post, parameters: parameters,
            encoding: encoding, headers: headers
        )
        .validate()
        .responseData { response in

            self.logger.info(
                "POST Response: \(response.response?.statusCode as Any)"
            )

            let decoder = JSONDecoder()
            decoder.dateDecodingStrategy = .custom(Date.toTMDBDate)

            // if response.result is successful data, try to decode.
            // if decoding fails, result is that error.
            // if response.result is failure, result is that error.
            let result = response.result.flatMap {
                try decoder.decode(TResponseData.self, from: $0)
            }
            .ifFailure {
                self.logger.error($0)
            }

            completion(result)
        }
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)

答案 2 :(得分:0)

我通过将tapRecognizer选择器移动到我之前设置的触摸处理程序函数中解决了我的问题,因此代码现在看起来像这样:

private func handleTouches(touches: Set<UITouch>) {
    for touch in touches {
        let touchLocation = touch.locationInNode(self)
        lastTouch = touchLocation

        let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
        tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)];
        self.view!.addGestureRecognizer(tapRecognizer)
    }
}

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Ended {
        print("Menu button released")
    }     
 }