首页
学习
活动
专区
圈层
工具
发布

TVOS大小
EN

Stack Overflow用户
提问于 2017-03-10 09:41:22
回答 2查看 1.1K关注 0票数 0

当使用ImageView聚焦时,可以调整imgView.adjustsImageWhenAncestorFocused = true的大小/帧吗?

一直在浏览网页,但找不到任何设置缩放大小的效果,似乎只是一些默认值。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-10 13:44:33

看来你不能这么做。我认为苹果不允许这样做是有原因的。

这里有很多tvOS的详细人机界面指南。它们建议具有不同列数的网格布局的间距和项大小,以便查看体验是最佳的:

下面的网格布局提供了最佳的查看体验。确保在未对焦的行和列之间使用适当的间距,以防止在将项引入焦点时出现重叠。

我想焦点UIImageView的“默认”框架考虑到了这些推荐的项目大小。苹果不允许改变它,因为它可能会引起问题,就像其他网格项目被重叠一样。

因此,您不能修改聚焦UIImageView的框架,但是可以通过使用focusedFrameGuide 属性间接地访问它。

票数 2
EN

Stack Overflow用户

发布于 2020-06-26 13:34:53

您可以通过imgView.transform调整大小。如果您的imgView在另一个视图中(例如在UICollectionViewCell中),您可以使用下面的代码在接收焦点时将图像缩小10%

代码语言:javascript
复制
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    super.didUpdateFocus(in: context, with: coordinator)
    if context.nextFocusedView === self {
        coordinator.addCoordinatedAnimations({
            self.imgView.transform = self.transform.scaledBy(x: 0.9, y: 0.9)
        }, completion: nil)
    }
    if context.previouslyFocusedView === self {
        coordinator.addCoordinatedAnimations({
            self.imgView.transform = .identity
        }, completion: nil)
    }
}

此外,您还可以用下面的代码使用UIImageView计算adjustsImageWhenAncestorFocused = true的系统焦点标度:

代码语言:javascript
复制
let xScale = imgView.focusedFrameGuide.layoutFrame.size.width / imgView.frame.size.width 
let yScale = imgView.focusedFrameGuide.layoutFrame.size.height / imgView.frame.size.height 

如果您想在使用UIImageView关注adjustsImageWhenAncestorFocused = true时删除缩放,请使用:

代码语言:javascript
复制
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    super.didUpdateFocus(in: context, with: coordinator)
    let xScale = imgView.focusedFrameGuide.layoutFrame.size.width / imgView.frame.size.width 
    let yScale = imgView.focusedFrameGuide.layoutFrame.size.height / imgView.frame.size.height 
    if context.nextFocusedView === self {
        coordinator.addCoordinatedAnimations({
            self.imgView.transform = self.transform.scaledBy(x: 1 / xScale, y: 1 / yScale)
        }, completion: nil)
    }
    if context.previouslyFocusedView === self {
        coordinator.addCoordinatedAnimations({
            self.imgView.transform = .identity
        }, completion: nil)
    }
}

别忘了把clipsToBounds = false设置在UIImageView

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42715047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档