2016-12-23 67 views
0

我在制作tvOS应用程序。我有一个集合视图单元格,它有一个UIImageView。我在界面生成器中选中“调整聚焦图像”,以便我可以使用系统为图像提供的聚焦效果。问题是我还为图像设置了边框。当视图获得焦点并且图像放大时,边框不会与图像一起放大。任何想法如何在视图集中时更新边框的尺寸?AppleTV重点图像边框不缩放

回答

1

我想你应该把你的imageView放在UIView的对象中,然后将边框放到这个视图中。之后,您应该重写“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 
} 

所以,通过这种方式,您可在图像视图给人以边境动画。