gpt4 book ai didi

swift - 识别 Apple TV Remote 上的选择

转载 作者:行者123 更新时间:2023-11-28 06:28:22 24 4
gpt4 key购买 nike

我的 Apple tv 应用正在从 URL 中提取音频并播放。但是,再次按下按钮时需要暂停。

因此我向 UIButton 添加了一个 addTarget 以查看它是否正在播放音频。

这是在我的 ViewDidLoad 中

player = AVPlayer(url:url)
let playerLayer = AVPlayerLayer(player: player)
let rect = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 10, height: 50))
playerLayer.frame = rect
self.view.layer.addSublayer(playerLayer)
playButton.addTarget(self, action: "plays:", for: UIControlEvents.primaryActionTriggered )

函数播放

func plays(sender: UIButton!){
NSLog("Clicked")
if player?.rate == 0{
player!.play()
print("<PLAYING")
}else{
player!.pause()
}
}

但是这给了我以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController plays:]: unrecognized selector sent to instance 0x7fe917802b80'

我如何识别 Remote 上的“点击”或“按下”并使函数 play 发挥作用?

最佳答案

在 Swift 3 中,您需要在方法的第一个参数前添加下划线。

func plays(_ sender: UIButton!) { 

以及正确的选择器语法:

#selector(plays(_:))

关于swift - 识别 Apple TV Remote 上的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41093581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com