使UILabel可调焦和可点击(tvOS) [英] Make UILabel focusable and tappable (tvOS)

查看:125
本文介绍了使UILabel可调焦和可点击(tvOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现6行高描述标签,我希望它是可以聚焦的。理想情况下,这意味着要扩展UILabel类来创建自定义组件。我试图通过执行 canBecomeFocused didUpdateFocusInContext ,但我的UILabel似乎没有得到任何焦点。



我也尝试用UIButton替换UILabel,但是按钮并没有为这类事情进行真正的优化。此外,这将意味着我需要改变焦点从自定义到纯粹的 buttonType 和buttonType似乎是一个现成的属性。



实际上,我希望苹果在Apple TV Movies应用程序中使用完全相同的文本标签。对于电影描述他们有一个文本标签,显示几行文字和一个更多。聚焦时,它看起来像一个按钮(阴影周围),并改变背景颜色。当点击 - 它打开一个模式窗口与整个电影说明。





有什么建议吗?或者也许有人已经实现了这个自定义控制tvOS?或事件更好 - 有一个从苹果公司的组件,这样做,我失去了一些东西。



PS:Swift解决方案将是受欢迎的:)

解决方案

好的,回答我自己的问题:)

可重点在tvOS开箱即用,其他必须被指示这样做。

我终于使用了 UITextView ,它有一个可选的属性,但如果不是这些默认的可聚焦视图之一。编辑 TextView 必须禁用,使其看起来像 UILabel 。此外,目前有一个错误,它可以防止你使用InterfaceBuilder中的 selectable 属性,但是可以从代码中使用



当然,也必须实现 canBecomeFocused() didUpdateFocusInContext 。您还需要传递一个 UIViewController ,因为 UITextView 不能呈现模态视图控制器。 Bellow是我最终创建的。

  class FocusableText:UITextView {

var data:String?
var parentView:UIViewController?

覆盖func awakeFromNib(){
super.awakeFromNib()
let tap = UITapGestureRecognizer(target:self,action:tapped:)
tap.allowedPressTypes



func(手势:UITapGestureRecognizer){
let storyboard = UIStoryboard(名称:Main,bundle:nil)
如果让descriptionView = storyboard.instantiateViewControllerWithIdentifier(descriptionView)为? DescriptionViewController {
if view = parentView {
if show = show {
descriptionView.descriptionText = self.data
view.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen
view。 () - > presentViewController(descriptionView,animated:true,completion:nil)
}
}
}
}

覆盖func canBecomeFocused Bool {
return true
}

覆盖func didUpdateFocusInContext(context:UIFocusUpdateContext,withAnimationCoordinator协调器:UIFocusAnimationCoordinator){

if context.nextFocusedView == self {
coordinator.addCoordinatedAnimations({() - >
中的无效self.layer.backgroundColor = UIColor.blackColor()。colorWithAlphaComponent(0.2).CGColor
},completion:nil)
} else if context.previouslyFocusedView == self {
coordinator.addCoordinatedAnimations({() - > Void in
self.layer.backgroundColor = UIColor.clearColor()。CGColor
},完成:无)
}
}

}


I'm trying to implement 6 lines high description label and I want it to be focusable. Ideally that would mean extending UILabel class to make a custom component. I tried that by implementing canBecomeFocused and didUpdateFocusInContext but my UILabel doesn't seem to get any focus.

I also tried replacing UILabel with UIButton, but buttons aren't really optimised for this sort of thing. Also that would mean I'd need to change buttonType on focus from custom to plain.. and buttonType seems to be a ready-only property.

In reality I'd like to have exact same text label implemented by Apple in Apple TV Movies app. For movie description they have a text label that displays a few lines of text and a "more". When focused it looks like a button (shadows around) and changed background color. When tapped - it opens up a modal window with entire movie description.

Any suggestions? Or maybe someone has already implemented this custom control for tvOS? Or event better - there is a component from Apple that does this and I'm missing something.

P.S: Swift solution would be welcome :)

解决方案

Ok, answering my own question :)

So it appears that some some views are "focusable" on tvOS out-of-the-box, and other have to be instructed to do so.

I finally ended up using UITextView, which has a selectable property, but if not one of these focusable views by default. Editing of TextView has to be disabled to make it look like UILabel. Also, currently there is a bug which prevents you from using selectable property from Interface Builder but works from code.

Naturally, canBecomeFocused() and didUpdateFocusInContext had to be implemented too. You'll also need to pass a UIViewController because UITextView is not capable of presenting a modal view controller. Bellow is what I ended up creating.

class FocusableText: UITextView {

    var data: String?
    var parentView: UIViewController?

    override func awakeFromNib() {
        super.awakeFromNib()
        let tap = UITapGestureRecognizer(target: self, action: "tapped:")
        tap.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
        self.addGestureRecognizer(tap)
    }

    func tapped(gesture: UITapGestureRecognizer) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if let descriptionView = storyboard.instantiateViewControllerWithIdentifier("descriptionView") as? DescriptionViewController {
            if let view = parentView {
                if let show = show {
                    descriptionView.descriptionText = self.data
                    view.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen
                    view.presentViewController(descriptionView, animated: true, completion: nil)
                }
            }
        }
    }

    override func canBecomeFocused() -> Bool {
        return true
    }

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

        if context.nextFocusedView == self {
            coordinator.addCoordinatedAnimations({ () -> Void in
                self.layer.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.2).CGColor
            }, completion: nil)
        } else if context.previouslyFocusedView == self {
            coordinator.addCoordinatedAnimations({ () -> Void in
                self.layer.backgroundColor = UIColor.clearColor().CGColor
            }, completion: nil)
        }
    }

}

这篇关于使UILabel可调焦和可点击(tvOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆