【问题标题】:Add action to UIButton when focused (swift)聚焦时向 UIButton 添加操作(快速)
【发布时间】:2015-10-20 17:23:11
【问题描述】:

尝试在 UIButton 获得焦点时向其添加操作 (tvOS)。

我正在使用

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
       super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)

然后:

    if self.button1.focused == true {
    //do something
    }

  if self.button2.focused == true {
    //do something else
    }

这是最好的方法还是我会遇到麻烦?

编辑:

您认为这是正确的还是有更好的方法?

 override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
            super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
            coordinator.addCoordinatedAnimations({ () -> Void in

                var previewPlayer = AVPlayer()
                let previewPlayerLayer = AVPlayerLayer(player: previewPlayer)

                previewPlayerLayer.frame = CGRectMake(self.view.frame.size.width/2-128, self.view.frame.size.height/2-254, 254, 128)
                previewPlayer.volume = 0

                 if self.button1.focused == true {

                    let videoURL = NSURL(string: "someString")!
                    previewPlayer = AVPlayer(URL: videoURL)

                }

                if self.button2.focused == true {

                    let videoURL = NSURL(string: "someOtherString")!
                    previewPlayer = AVPlayer(URL: videoURL)

                }


                  previewPlayer.play()


                }) { () -> Void in

            }
        }

【问题讨论】:

    标签: swift uibutton tvos


    【解决方案1】:

    您可以通过以下两种方式之一了解视图何时聚焦:

    //returns bool whether view is focused
    someview.focused 
    
    //returns focused view, can return nil
    UIScreen.mainScreen().focusedView
    

    这些方法中的任何一种都是正确的,不会给您带来麻烦;)

    【讨论】:

    • 谢谢。似乎当我添加一个子层(如果有焦点)并删除(如果不是)时正在分解。不知道为什么...
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多