关键帧动画后图像消失

时间:2021-03-01 06:20:01

标签: ios swift core-animation

我有一个动画,其中绘制了一条线,然后移动了另一个图像。动画效果很好,但随着动画完成,图像从视图中消失。动画完成后图像应保留。我的动画代码:

private func addPlaneAnimation() {
    let image = UIImageView(frame: CGRect(x: -30, y: view.height*0.8, width: 30, height: 30))
    image.image = R.image.plane()
    view.addSubview(image)
    
    let path = UIBezierPath()
    path.move(to: image.center)
    path.addLine(to: CGPoint(x: (view.width*0.85), y: (view.height*0.40)))
    
    let shapeLayer = CAShapeLayer()
    shapeLayer.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
    shapeLayer.strokeColor = R.color.Purple()?.cgColor
    shapeLayer.lineWidth = 3
    shapeLayer.path = path.cgPath
    view.layer.addSublayer(shapeLayer)
    
    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = 0
    animation.duration = 4
    animation.beginTime = CACurrentMediaTime() + 0.2
    animation.isRemovedOnCompletion = false
    
    let pathAnimation = CAKeyframeAnimation(keyPath: "position")
    pathAnimation.path = path.cgPath
    pathAnimation.duration = 4
    pathAnimation.autoreverses = false
    pathAnimation.isRemovedOnCompletion = false
    
    shapeLayer.add(animation, forKey: "MyAnimation")
    image.layer.add(pathAnimation, forKey: "position")
    view.bringSubviewToFront(cardBarcode)
}

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

只需再添加两个属性即可。

对于 strokeEnd 动画

animation.fillMode = CAMediaTimingFillMode.backwards

和路径动画

pathAnimation.fillMode = CAMediaTimingFillMode.forwards

enter image description here

相关问题