在 tvOS 12 的 TVUIKit 中使用 TVPosterImage [英] Using TVPosterImage in TVUIKit for tvOS 12

查看:27
本文介绍了在 tvOS 12 的 TVUIKit 中使用 TVPosterImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tvOS 12 有一个新框架 TVUIKit,它引入了锁定视图.我感兴趣的类是TVPosterView,它基本上是这样设计的:>

Swift 4.2

open class TVPosterView : TVLockupView {//可以使用 UIControl 在 iOS 中实现公共初始化(图像:UIImage?)打开 var 图像:UIImage?//默认为零打开 var 标题:字符串?打开 var 副标题:字符串?}

在我的故事板中,我添加了一个 UIView 元素,并且正确地(至少我希望如此.昨天 IB 代理不断崩溃!)在 Identity Inspector 中将其类更改为 TVPosterView.然后在我的 UIViewController 中定义:

@IBOutlet var aPosterView:TVPosterView!覆盖 func viewDidLoad() {super.viewDidLoad()if let myIcon = UIImage(named: "icon.png") {self.aPosterView = TVPosterView(图片:myIcon)self.aPosterView.title = "我的标题"self.aPosterView.subtitle = "副标题"}别的 {打印(错误:我无法加载图标!")}}}

它编译时没有任何警告.当我运行它时,它只显示 UIView 白色背景,没有任何变化.我做了一些尝试添加 UIImageView 的测试,但它们都没有结果(当然,当我将 UIImageView 的图像设置为 self.aPosterView.image 时).

我远非专家,我几周前才开始学习.知道我缺少什么吗?我认为这个概念是,一旦我开始上课,框架就会负责显示带有(可选)标题和副标题的海报,还负责所有漂亮的动画!

解决方案

最终我设法使其以编程方式工作.我测试了 TVPosterViewTVMonogramView(基本上是一个圆形按钮).以下代码适用于 tvOS 12 beta 5 上的 Xcode 10 beta 5:

Swift 4.2

导入 UIKit导入 TVUIKit类 SecondViewController: UIViewController {var myPoster = TVPosterView()var myMonogram = TVMonogramView()覆盖 func viewDidLoad() {super.viewDidLoad()myPoster.frame = CGRect(x: 100, y: 100, width: 550, height: 625)myPoster.image = UIImage(named: "image1.png")myPoster.imageView.masksFocusEffectToContents = truemyPoster.title = "海报"myPoster.subtitle = "这是海报副标题"self.view.addSubview(myPoster)var myName = PersonNameComponents()myName.givenName = "米歇尔"myName.familyName = "Dall'Agata"myMonogram.frame = CGRect(x: 700, y: 100, width: 500, height: 475)myMonogram.image = UIImage(named: "image2.png")myMonogram.title = "字母组合"myMonogram.subtitle = "这是会标字幕"myMonogram.personNameComponents = myNameself.view.addSubview(myMonogram)打印(myMonogram.personNameComponents)}}

不过,我没有设法使用 scaleAspectFit 缩放海报的图像.此外,我的第一张图片是圆角的,并且只选择了一个与方形图片和标题完美匹配的帧大小(因此不需要纵横比),发光效果在角落上变得透明.否则整个图像是不透明的,使用它自己的(相当小的)圆角.

tvOS 12 has a new framework TVUIKit, which introduces the lockup views. The class I am interested in is TVPosterView, which is basically designed as such:

Swift 4.2

open class TVPosterView : TVLockupView { // One may use UIControl to implement it in iOS
    public init(image: UIImage?)
    open var image: UIImage? // default is nil
    open var title: String?
    open var subtitle: String?
}

In my storyboard I have added an UIView element, and rightly (at least I hope so. Yesterday the IB agent kept crashing!) changed its class to TVPosterView in Identity Inspector. then in my UIViewController I have defined:

@IBOutlet var aPosterView: TVPosterView!

override func viewDidLoad() {
        super.viewDidLoad()

        if let myIcon = UIImage(named: "icon.png") {
            self.aPosterView = TVPosterView(image: myIcon)
            self.aPosterView.title = "My Title"
            self.aPosterView.subtitle = "A Sub Title"
        }
        else {
            print("ERROR: I couldn't load the icon!")
        }
    }
}

It compiles without any warning. When I run it, it just shows the UIView white background, nothing changes. I did some tests trying to add an UIImageView, but they were all inconclusive (apart, off course, when I did set the image of UIImageView to self.aPosterView.image).

I am far from being an expert, I just started learning a couple of weeks ago. Any idea about what I am missing? I thought the concept was that once I initiated the class the framework was taking care of displaying the poster with the (optional) title and subtitles, also taking care of all nice animations!

解决方案

Eventually I managed to make it working programmatically. I tested both TVPosterView and TVMonogramView (basically a round button). The following code works with Xcode 10 beta 5, on tvOS 12 beta 5:

Swift 4.2

import UIKit
import TVUIKit

class SecondViewController: UIViewController {

    var myPoster = TVPosterView()
    var myMonogram = TVMonogramView()

    override func viewDidLoad() {
        super.viewDidLoad()

        myPoster.frame = CGRect(x: 100, y: 100, width: 550, height: 625)
        myPoster.image = UIImage(named: "image1.png")
        myPoster.imageView.masksFocusEffectToContents = true
        myPoster.title = "Poster"
        myPoster.subtitle = "This is the poster subtitle"

        self.view.addSubview(myPoster)

        var myName = PersonNameComponents()
        myName.givenName = "Michele"
        myName.familyName = "Dall'Agata"

        myMonogram.frame = CGRect(x: 700, y: 100, width: 500, height: 475)
        myMonogram.image = UIImage(named: "image2.png")
        myMonogram.title = "Monogram"
        myMonogram.subtitle = "This is the Monogram subtitle"
        myMonogram.personNameComponents = myName

        self.view.addSubview(myMonogram)

        print(myMonogram.personNameComponents)
    }
}

I didn't manage to scale the poster's image with scaleAspectFit, though. Also my first image was rounded with a transparency, and only choosing a frame size that perfectly fit the squared image plus the titles (so no aspect fit needed), the glowing effect became transparent on the corners. Otherwise the whole image was opaque, using its own (quite small) rounded corners.

这篇关于在 tvOS 12 的 TVUIKit 中使用 TVPosterImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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