我正在制作 tvOS 应用。我有一个 Collection View 单元格,它有一个 UIImageView。我在界面生成器中选中“聚焦时调整图像”,这样我就可以使用系统为图像提供的聚焦效果。问题是我还为图像设置了边框。当 View 获得焦点并且图像按比例放大时,边框不会随着图像按比例放大。有什么想法可以在 View 聚焦时更新边框的尺寸吗?
最佳答案
我认为您应该将 imageView 放在 UIView 的对象中,然后为该 View 提供边框。之后你应该覆盖“didUpdateFocus”方法并根据需要制作动画。 看, 在我的例子中,我将 imageView 放在“viewBg”对象中并为该对象提供动画。
在你的类里面:
func viewAnimation(view:UIView , isNormal:Bool)
{
UIView.animate(withDuration: 0.5, animations: { () -> Void in
if isNormal{
view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0);
}
else{
view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3);
}
})
{ (finished) -> Void in
}
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
if(context.previouslyFocusedView != nil)
{
if (context.previouslyFocusedView is testCell)
{
let cell:testCell = context.previouslyFocusedView as! testCell
self.viewAnimation(view: cell.viewBg, isNormal: true)
}
}
if(context.nextFocusedView != nil)
{
if (context.nextFocusedView is testCell)
{
let cell:testCell = context.nextFocusedView as! testCell
self.viewAnimation(view: cell.viewBg, isNormal: false)
}
}
}
在您的 CollectionViewCell 方法中:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:testCell = collectionView.dequeueReusableCell(withReuseIdentifier: "testCell", for: indexPath) as! testCell
cell.imgIcon.image = UIImage(named:arrayIconImages.object(at: indexPath.row) as! String)
cell.viewBg.layer.borderColor = UIColor.black.cgColor
cell.viewBg.layer.borderWidth = 10.00
return cell
}
因此,通过这种方式,您可以在 ImageView 中提供带边框的动画。
关于swift - AppleTV 聚焦图像边框不缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41299790/