加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 简单的tvOS UIButton无效

发布时间:2020-12-14 04:59:12 所属栏目:百科 来源:网络整理
导读:我正在尝试实现一个简单的UIButton(tvOS Swift),但TouchDown事件没有触发. (也试过TouchUpInside). 在模拟器中,我可以直观地看到按下按钮. 我的ViewController.swift代码是: import UIKitclass ViewController: UIViewController { override func viewDidLo
我正在尝试实现一个简单的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")
    }
}

为了检测按钮点击,我需要做什么?

解决方法

您不应该在tvOS上使用“.TouchDown”或“.TouchUpInside”,因为这不符合您的预期:您可能希望使用“UIControlEvents.PrimaryActionTriggered”.

TouchUpInside是由实际触摸触发的,实际上并非如此.如果要按遥控器上的“选择”按钮以触发按钮,则应使用PrimaryActionTriggered.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读