当使用ImageView聚焦时,可以调整imgView.adjustsImageWhenAncestorFocused = true的大小/帧吗?
一直在浏览网页,但找不到任何设置缩放大小的效果,似乎只是一些默认值。
发布于 2017-03-10 13:44:33
看来你不能这么做。我认为苹果不允许这样做是有原因的。
这里有很多tvOS的详细人机界面指南。它们建议具有不同列数的网格布局的间距和项大小,以便查看体验是最佳的:
下面的网格布局提供了最佳的查看体验。确保在未对焦的行和列之间使用适当的间距,以防止在将项引入焦点时出现重叠。
我想焦点UIImageView的“默认”框架考虑到了这些推荐的项目大小。苹果不允许改变它,因为它可能会引起问题,就像其他网格项目被重叠一样。
因此,您不能修改聚焦UIImageView的框架,但是可以通过使用focusedFrameGuide 属性间接地访问它。
发布于 2020-06-26 13:34:53
您可以通过imgView.transform调整大小。如果您的imgView在另一个视图中(例如在UICollectionViewCell中),您可以使用下面的代码在接收焦点时将图像缩小10%
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的系统焦点标度:
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时删除缩放,请使用:
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上
https://stackoverflow.com/questions/42715047
复制相似问题