tvOS/Swift 3:如何修复损坏的UICollectionView选择动画?

tvOS/Swift 3:如何修复损坏的UICollectionView选择动画?,swift,uicollectionview,uicollectionviewcell,tvos,Swift,Uicollectionview,Uicollectionviewcell,Tvos,我对UICollectionViewCell进行了子类化,这样视差效果可以应用于整个单元格,而不仅仅是单元格中的图像。视差效果按我所希望的那样工作,但它会破坏在选定单元格上按下遥控按钮时发生的按钮式动画 collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath)被调用,因为它应该是远程按钮,只是没有任何动画 我想我必须做动画,但不知道如何触发它,或者,实际上,如何做 这是我的UIColle

我对UICollectionViewCell进行了子类化,这样视差效果可以应用于整个单元格,而不仅仅是单元格中的图像。视差效果按我所希望的那样工作,但它会破坏在选定单元格上按下遥控按钮时发生的按钮式动画

collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath)被调用,因为它应该是远程按钮,只是没有任何动画

我想我必须做动画,但不知道如何触发它,或者,实际上,如何做

这是我的UICollectionViewCell:

class CollectionViewCellA: UICollectionViewCell {

@IBOutlet var image: UIImageView!
@IBOutlet var bkgImage: UIImageView!
@IBOutlet var title: UILabel!

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {

    struct wrapper {
        static let s_atvMotionEffect = UIAppleTVMotionEffectGroup()
    }

    coordinator.addCoordinatedAnimations( {
        var scale : CGFloat = 0.0
        if self.isFocused {
            self.addMotionEffect(wrapper.s_atvMotionEffect)
            self.layer.shadowOpacity = 0.75;
            self.layer.shadowRadius = 32.0;
            self.layer.shadowOffset = CGSize(width: 0, height: 16);
            scale = 1.2
        } else {
            self.removeMotionEffect(wrapper.s_atvMotionEffect)
            self.layer.shadowOpacity = 0.25;
            self.layer.shadowRadius = 4.0;
            self.layer.shadowOffset = CGSize(width: 0, height: 0);
            scale = 1.0
        }
        let transform = CGAffineTransform(scaleX: scale, y: scale)
        self.layer.setAffineTransform(transform)
    },completion: nil)

   }
}
这是UICollectionView所在的UIViewController:

class HomeVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {


    @IBOutlet var freeContentCV: UICollectionView!
    @IBOutlet var collectionViewB: UICollectionView!
    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet var mainImage: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        freeContentCV.delegate = self
        collectionViewB.delegate = self

        freeContentCV.dataSource = self
        collectionViewB.dataSource = self
    }


    override func viewDidLayoutSubviews() {
        self.scrollView!.contentSize = CGSize(width: 1920, height: 2500)
    }

    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        //print(UIScreen.main.focusedView)
    }

    // Collection View Methods

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if collectionView == self.freeContentCV{
        return 10
       }

     return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if collectionView == self.freeContentCV {
        let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "CellA", for: indexPath) as! CollectionViewCellA
        cellA.bkgImage.image = UIImage(named: "vincent")
        cellA.title.text = "TITLE"
        return cellA
        }

        let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "CellB", for: indexPath) as! CollectionViewCellA
        cellB.image.image = UIImage(named: "vincent")
        cellB.title.text = "TITLE"
        return cellB
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

       // What goes here to trigger an animation, if anything? 
    }

}
我假设didSelectItemAt函数是我必须触发按钮动画的地方,但我不知道如何访问UICollectionViewCell来设置动画,或者即使按照我的安排这是可能的。我对iOS/tvOS的理解还处于边缘,所以我不确定该问什么问题

我查阅了很多资料,还没有发现关于这个问题的任何讨论


任何想法都得到了广泛的接受。

在今天早上对这个问题进行了一次新的尝试之后,以下是有效的方法

在UICollectionViewCell中

class CollectionViewCellA: UICollectionViewCell {

@IBOutlet var image: UIImageView!
@IBOutlet var bkgImage: UIImageView!
@IBOutlet var title: UILabel!

var selectTrans: UIFocusAnimationCoordinator?
var scale : CGFloat = 0.0

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {

    struct wrapper {
        static let s_atvMotionEffect = UIAppleTVMotionEffectGroup()
    }

    coordinator.addCoordinatedAnimations( {
        if self.isFocused {
            self.addMotionEffect(wrapper.s_atvMotionEffect)
            self.layer.shadowOpacity = 0.75;
            self.layer.shadowRadius = 10.0;
            self.layer.shadowOffset = CGSize(width: 0, height: 5);
            self.scale = 1.2
            let transform = CGAffineTransform(scaleX: self.scale, y: self.scale)
            self.layer.setAffineTransform(transform)
        } else {
            self.removeMotionEffect(wrapper.s_atvMotionEffect)
            self.layer.shadowOpacity = 0.25;
            self.layer.shadowRadius = 4.0;
            self.layer.shadowOffset = CGSize(width: 0, height: 0);
            self.scale = 1.0
            let transform = CGAffineTransform(scaleX: self.scale, y: self.scale)
            self.layer.setAffineTransform(transform)
        }
    },completion: nil)

}

func startSelectAnimation() {
    scale = 1.1
    let transform = CGAffineTransform(scaleX: scale, y: scale)
    self.layer.setAffineTransform(transform)
    perform(#selector(middleAnimation), with: nil, afterDelay: 0.08)
}

func middleAnimation() {
    scale = 0.95
    let transform = CGAffineTransform(scaleX: scale, y: scale)
    self.layer.setAffineTransform(transform)
    perform(#selector(endSelectAnimation), with: nil, afterDelay: 0.1)
}

func endSelectAnimation(){
    scale = 1.2
    let transform = CGAffineTransform(scaleX: scale, y: scale)
    self.layer.setAffineTransform(transform)
}
}
在ViewController中

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

         // We have to provide animation, so get the cell and do call to animation
         let selectedCell = collectionView.cellForItem(at: indexPath) as! CollectionViewCellA
         selectedCell.startSelectAnimation()
         if collectionView == self.freeContentCV {
         performSegue(withIdentifier: "ShowVideoDetail", sender: self)
     }
    }
动画有点笨重,但当它触发一个序列时,抖动并不那么明显