wlt*_*rup 6 animation uiview uiviewanimation ios swift
我有一个超视图A和一个子视图B.在A中drawRect(_:)我做了一些线图,它取决于子视图B的位置和大小.在A类(但不在其中drawRect(_:))我还有一个移动和缩放B的动画块.
问题是绘图drawRect(_:)总是在动画发生之前发生,要么使用子视图的最终位置和大小,要么使用其初始位置和大小,但从不使用中间值.
案例1:绘图使用最终状态
下图是模拟器的屏幕截图,而缩放子视图的动画正在进行中.注意线条是如何在最终状态下绘制的.
线条图的代码在A中drawRect(_:).这是动画的代码(它在A类self中指的是superview):
UIView.animateWithDuration(3.0, delay: 0.5,
options: UIViewAnimationOptions.CurveLinear,
animations: {
[unowned self] in
self.subviewWidthConstraint.constant = 0.75 * CGRectGetWidth(self.bounds)
self.layoutIfNeeded()
self.setNeedsDisplay() // this has no effect since self's bounds don't change
}) { (completed) -> Void in }
Run Code Online (Sandbox Code Playgroud)

案例2:绘图使用初始状态
寻找答案我发现了一个建议用于self.layer.displayIfNeeded()代替self.setNeedsDisplay()动画块,所以我也试过了
func animateFrame()
{
UIView.animateWithDuration(3.0, delay: 0.5,
options: UIViewAnimationOptions.CurveLinear,
animations: {
[unowned self] in
self.subviewWidthConstraint.constant = 0.75 * CGRectGetWidth(self.bounds)
self.layoutIfNeeded()
self.layer.displayIfNeeded() // the only difference is here
}) { (completed) -> Void in }
}
Run Code Online (Sandbox Code Playgroud)
但它导致以下结果.同样,这是一个屏幕截图,而放大子视图的动画正在进行中.现在,在动画开始之前再次绘制线条,但使用子视图的初始位置和大小.

我想我理解了案例1.整个动画块排队等待执行,但它的最终结果已经在动画开始时计算出来,所以drawInRect(_:)使用最终状态也就不足为奇了.
我不理解案例2,但那是因为我没有直接处理视图层的经验.
无论哪种方式,我期待达到的效果是,线被描绘为子视图的顶点,而它的移动和/或缩放.任何建议或文件指针都非常感谢.谢谢!