tvOS按钮操作

时间:2016-07-16 11:45:33

标签: swift uibutton tvos

我通过Swift在tvOS中创建了一个UIButton

<TouchableOpacity onPress={this.getMarkers}>
          <Text style={styles.text}>Press Me</Text>
        </TouchableOpacity>

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

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)

2 个答案:

答案 0 :(得分:9)

在tvOS for Button操作UIControlEvents TouchUpInside不会调用。

您必须使用下面的UIControlEvents PrimaryActionTriggered

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

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

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

答案 1 :(得分:2)

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

解决了它,它只是另一个UIControlEvents