简体   繁体   English

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

[英]CATransaction completion block being called immediately when animation is added to subview

I'm trying to start animation on the newly added UIImageView as subview but it seems that completion block is called immediately in this case even though animations are added after the CATransaction.setCompletionBlock 我正在尝试在新添加的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()

Animations: 动画:

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
}

Any idea why is it not working? 知道为什么它不起作用吗?

Clearly, the animation is not on the rendering tree. 显然,动画不在渲染树上。 ie the view has no superView . 即视图没有superView or somewhere its superView has no superView . 或其superView没有superView地方。 You may need to check whether the view implementing functions was added to a window or view somewhere. 您可能需要检查实现视图的功能是否已添加到window或某处的view

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM