小编wlt*_*rup的帖子

强制UIView在动画块内重绘其内容

我有一个超视图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 …
Run Code Online (Sandbox Code Playgroud)

animation uiview uiviewanimation ios swift

6
推荐指数
0
解决办法
813
查看次数

标签 统计

animation ×1

ios ×1

swift ×1

uiview ×1

uiviewanimation ×1