【发布时间】:2023-01-19 20:53:36
【问题描述】:
在我的 UICollectionViewCell 中,我通过 UIBezierPath 添加了一个带有动画的边框,如下所示。
class AnimalSelectionCollectionCell: UICollectionViewCell {
...
let shapeLayer: CAShapeLayer = CAShapeLayer()
override func layoutSubviews() {
super.layoutSubviews()
let path: UIBezierPath = UIBezierPath(
roundedRect: contentView.bounds.inset(by: UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)),
byRoundingCorners: .allCorners, cornerRadii: CGSize(width: contentView.bounds.size.width, height: 0.0))
shapeLayer.path = path.cgPath
}
func setData() {
...
shapeLayer.lineWidth = 1.0
shapeLayer.fillColor = UIColor.clear.cgColor
contentView.layer.addSublayer(shapeLayer)
}
func startAnimation() {
shapeLayer.strokeColor = AppColors.Text.selectedName.cgColor
let animation: CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = 0.5
shapeLayer.add(animation, forKey: "myStroke")
}
}
在选择 UICollectionViewCell 时,我正在调用 startAnimation() 方法,该方法使用动画绘制边框。现在,再次选择时,我想以相反的顺序(即从 1.0 到 0.0)删除带有动画的边框。
如何删除带动画的绘制路径?
【问题讨论】:
标签: ios swift cabasicanimation