当前位置 : 主页 > 手机开发 > 其它 >

swift – tvOS Button Action

来源:互联网 收集:自由互联 发布时间:2021-06-11
我通过 Swift在tvOS中创建了一个UIButton let randomBtn = UIButton()randomBtn.setTitle("Zufällig", forState: .Normal)let RndNormal = UIImage(named: "RndNormal")let RndHoover = UIImage(named: "RndHoover")randomBtn.setImage(RndNormal,
我通过 Swift在tvOS中创建了一个UIButton

let randomBtn = UIButton()
randomBtn.setTitle("Zufällig", forState: .Normal)
let RndNormal = UIImage(named: "RndNormal")
let RndHoover = UIImage(named: "RndHoover")
randomBtn.setImage(RndNormal, forState: .Normal)
randomBtn.setImage(RndHoover, forState: .Focused)
randomBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
randomBtn.setTitleColor(UIColor.whiteColor(), forState: .Focused)

randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.TouchUpInside)

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
randomBtn.frame = CGRect(x: screenWidth - 150, y: 60 , width: 70 , height: 70)
self.view.addSubview(randomBtn)

但如果按下按钮,动作永远不会被解雇,tvOS有什么不同吗?

func click(sender: UIButton) {
    print("click")
}
在tvOS for Button动作UIControlEvents中,TouchUpInside不会调用.

您必须使用如下所示的UIControlEvents PrimaryActionTriggered.

randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)

如果您有任何疑惑,请参阅此链接

https://forums.developer.apple.com/thread/17925

网友评论