UIView.animation问题

时间:2017-11-02 13:03:22

标签: swift3 ios11 uianimation

UIView动画问题:完成动画后,会有另一个动画关闭视图,但在视图上留下一些线条如何解决?

enter image description here

let frameHeight: CGFloat = 44.0

self.frame = CGRect(x: 0, y: -frameHeight, width: UIScreen.main.bounds.width, height: frameHeight)

UIView.animate(withDuration: 0.5, animations: {

            self.frame.origin.y += self.frameHeight

        }, completion: { _ in

            UIView.animate(withDuration: 0.5, delay: 1, options: [], animations: {

                self.frame.origin.y -= self.frameHeight

            })

        })

1 个答案:

答案 0 :(得分:0)

在动画开始之前计算视图的帧,因为动画块不在与外部块上下文相同的runloop中运行。

此外,我注意到你在动画块中引用了self.frameHeight属性,而你在本地定义了另一个frameHeight

试试这个(预先计算的原点Y):

let frameHeight: CGFloat = 44.0
self.frame = CGRect(x: 0, y: -frameHeight, width: UIScreen.main.bounds.width, height: frameHeight)

// Calcuate destinations before
let finalOriginDestinationY = self.frame.origin.y
let firstOriginDestinationY = self.frame.origin.y + frameHeight

UIView.animate(withDuration: 0.5, animations: {

    // Set absolute value - otherwise the frame is calculated according to position at current time
    self.frame.origin.y = firstOriginDestinationY
}, completion: { _ in
    UIView.animate(withDuration: 0.5, delay: 1, options: [], animations: {

        // Set absolute value (Precalculated)
        self.frame.origin.y = finalOriginDestinationY
    })

})