Diz*_*zzy 5 uibutton swift tvos
我正在尝试实现一个简单的UIButton(tvOS和Swift),但TouchDown事件没有触发.(也试过TouchUpInside).
我的ViewController.swift代码是:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: UIButtonType.System)
button.frame = CGRectMake(100, 100, 400, 150)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(button)
self.view.userInteractionEnabled = true
}
func buttonAction(sender:UIButton!){
NSLog("Clicked")
}
}
Run Code Online (Sandbox Code Playgroud)
为了检测按钮点击,我需要做什么?
Ere*_*rez 24
您不应该在tvOS上使用".TouchDown"或".TouchUpInside",因为这不符合您的预期:您可能希望使用"UIControlEvents.PrimaryActionTriggered".
TouchUpInside是由实际触摸触发的,实际上并非如此.如果要按遥控器上的"选择"按钮以触发按钮,则应使用PrimaryActionTriggered.