如何在UICollectionViewCell内添加动画

时间:2019-04-06 20:25:48

标签: swift core-animation uicollectionviewcell calayer

我正在尝试在UICollectionViewCell内添加循环加载动画。我已经将相同的代码添加到普通的UIViewController中,并且可以成功运行,但是在UICollectionViewCell上没有任何结果。

理想情况下,我希望在单元格可见时开始播放动画。

-1000984

对于UICollectionViewCell,我不确定将实际的动画代码放在哪里。我已经在init内尝试过了,但是跟踪层甚至没有显示出来。这是动画的代码:

let shapeLayer = CAShapeLayer()
let trackLayer = CAShapeLayer()

func setupBezierPaths() {
    let center = self.center
    let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)

    // Create a track layer
    trackLayer.path = circularPath.cgPath
    trackLayer.strokeColor = UIColor.lightGray.cgColor
    trackLayer.lineWidth = 10
    trackLayer.lineCap = CAShapeLayerLineCap.round
    trackLayer.fillColor = UIColor.clear.cgColor

    layer.addSublayer(trackLayer)

    // Create a layer that is animated
    shapeLayer.path = circularPath.cgPath
    shapeLayer.strokeColor = UIColor.green.cgColor
    shapeLayer.lineWidth = 10
    shapeLayer.strokeEnd = 0
    shapeLayer.lineCap = CAShapeLayerLineCap.round
    shapeLayer.fillColor = UIColor.clear.cgColor

    layer.addSublayer(shapeLayer)
}

1 个答案:

答案 0 :(得分:-1)

我认为您可以在CollectionView的这种方法中使用它。

 func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)

并使用cell.layer更改层