我希望能够中断正在对UIImageView进行动画处理的CABasicAnimation并从其原始起点重新开始,但是如果这样做,它会完全消失。
在我的代码中,我删除了动画和UIIMageView(如果存在),但这是动画完全消失的,直到再次运行代码。因此,每隔一次消失。
如果我在设置的持续时间内运行动画,并让其使用其完成处理程序将其删除,然后再次启动它,则它会重新正常运行。
它可能不会消失,但是会在屏幕外显示,因为它可能会保留其结束动画位置的新设置位置。但是,如果重新定义变量和UIImageView,那是什么原因呢?是CATransaction吗?
if let swipeArrowIMGView:UIImageView = self.view.viewWithTag(viewTags.swipeArrow.rawValue) as? UIImageView {
swipeArrowIMGView.layer.removeAllAnimations()
swipeArrowIMGView.removeFromSuperview()
}
let swipeArrowIMG:UIImage = UIImage(named: "swipeArrow")!
let swipeArrowIMGView:UIImageView = UIImageView(image: swipeArrowIMG)
swipeArrowIMGView.tag = viewTags.swipeArrow.rawValue
self.view.addSubview(swipeArrowIMGView)
swipeArrowIMGView.transform = .identity
swipeArrowIMGView.transform = swipeArrowIMGView.transform.translatedBy(x: 75,
y: 75)
let animateToX = 75
let animateToY = 200
CATransaction.begin()
let animation = CABasicAnimation(keyPath: "position")
let arrowX:CGFloat = swipeArrowIMGView.layer.position.x
let arrowY:CGFloat = swipeArrowIMGView.layer.position.y
animation.fromValue = [arrowX, arrowY]
animation.toValue = [animateToX, animateToY]
animation.duration = 1.4
animation.isRemovedOnCompletion = true
swipeArrowIMGView.layer.position = CGPoint(x: animateToX, y: animateToY)
CATransaction.setCompletionBlock{ () in
if let swipeArrow:UIImageView = self.view.viewWithTag(viewTags.swipeArrow.rawValue) as? UIImageView {
swipeArrow.removeFromSuperview()
}
}
swipeArrowIMGView.layer.add(animation, forKey: nil)
CATransaction.commit()