swift - AppleTV 聚焦图像边框不缩放

标签 swift uicollectionviewcell tvos

我正在制作 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/

相关文章:

swift - 在展开可选值 prepareForSegue 时意外发现 nil

ios - 如何将普通搜索栏更改为下图所示的搜索栏

ios - 如何在 CollectionView 中选择单个单元格并取消选择所有其他单元格

ios - UICollectionView 中的单元格间距 minimumInteritemSpacing

ios - UITableView 和 tvOS 的新方法

objective-c - 如何使用 tvOS 接收触摸?

ios - iOS 和 tvOS 应用程序上的相同 viewController,UIPickerViewDelegate 在 tvOS 上不可用

swift - Nsuserdefaults 删除值和键

android - 如何更改 Firebase 中已发布应用程序的包标识符?

ios - 动态 UICollectionView 单元格框架