将动画添加到子视图时,将立即调用CATransaction完成块

时间:2019-03-08 23:01:00

标签: ios swift caanimation

我正在尝试在新添加的UIImageView上以子视图的形式开始动画,但是即使在CATransaction.setCompletionBlock之后添加了动画,在这种情况下,似乎也会立即调用完成块

addSubview(imageView)

CATransaction.begin()

let posAnimation = positionAnimation(startPoint: startPoint, endPoint: endPoint, beginTime: beginTime, duration: duration)
let alpAnimation = alphaAnimation(beginTime: beginTime, duration: duration)

CATransaction.setCompletionBlock{ [weak self] in
print("deleting view")
//imageView.removeFromSuperview()
}

imageView.layer.add(posAnimation, forKey: nil)
imageView.layer.add(alpAnimation, forKey: nil)

CATransaction.commit()

动画:

private func positionAnimation(startPoint: CGPoint, endPoint:CGPoint, beginTime: CFTimeInterval, duration: CFTimeInterval) -> CAKeyframeAnimation {
    let positionAnimation = CAKeyframeAnimation(keyPath: "position")

    positionAnimation.path = customPath(middlePoint: startPoint, endPoint: endPoint).cgPath
    positionAnimation.isRemovedOnCompletion = false
    positionAnimation.duration = duration
    positionAnimation.beginTime = beginTime
    positionAnimation.fillMode = CAMediaTimingFillMode.forwards
    return positionAnimation
}

private func alphaAnimation(beginTime: CFTimeInterval, duration: CFTimeInterval) -> CAKeyframeAnimation {
    let alphaAnimation = CAKeyframeAnimation(keyPath: "opacity")
    alphaAnimation.isRemovedOnCompletion = false
    alphaAnimation.fillMode = CAMediaTimingFillMode.forwards
    alphaAnimation.values = [1.0, 1.0, 0.0]
    alphaAnimation.keyTimes = [0.0,0.5,1.0]
    alphaAnimation.duration = duration
    alphaAnimation.beginTime = beginTime
    return alphaAnimation
}

有人知道为什么它不起作用吗?

1 个答案:

答案 0 :(得分:0)

很显然,动画不在渲染树上。也就是说,该视图没有superView。或其superView的某个地方没有superView。您可能需要检查是否将实现视图的功能添加到某个地方的windowview上。