如何根据另一个视图为视图设置动画

How to animate view based on another view

在我的动画中,中间的锁向上移动的速度比顶部窗格快。如何设置中间锁的动画,使其沿着顶部窗格向上移动?更具体地说,我希望中间的锁中心在向上和离开屏幕时始终与顶部窗格底部对齐。

这是我目前的代码,lockBorderlockKeyhole 就是所谓的 "middle lock",与 topLock 相比,它向上移动的速度太快了:

@IBOutlet var topLock: UIImageView!
@IBOutlet var bottomLock: UIImageView!
@IBOutlet var lockBorder: UIImageView!
@IBOutlet var lockKeyhole: UIImageView!


override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    openLock()

}


func openLock() {

    UIView.animateWithDuration(0.5, delay: 1.0, options: [], animations: {
        self.lockKeyhole.transform = CGAffineTransformMakeRotation(CGFloat(3.14))
        }, completion: { _ in

            UIView.animateWithDuration(0.6, delay: 0.2, options: [], animations: {

                var topFrame = self.topLock.frame
                topFrame.origin.y -= topFrame.size.height

                var bottomFrame = self.bottomLock.frame
                bottomFrame.origin.y += bottomFrame.size.height


                var lockBorderFrame = self.lockBorder.frame
                lockBorderFrame.origin.y -= self.view.frame.height

                var lockKeyholeFrame = self.lockKeyhole.frame
                lockKeyholeFrame.origin.y -= self.view.frame.height

                self.topLock.frame = topFrame
                self.bottomLock.frame = bottomFrame
                self.lockKeyhole.frame = lockKeyholeFrame
                self.lockBorder.frame = lockBorderFrame

                }, completion: { finished in

            })
        })
}

您将 topFrame.origin.y 调整为 topFrame.size.height,但您将 lockBorderFrame.origin.ylockKeyholeFrame.origin.y 调整为 self.view.frame.height。我们可以推断出这些调整是不同的数额。我假设 topFrame.size.heightself.view.size.height 的二分之一。由于您将锁定视图移动的距离是移动距离的两倍 topLock,并且两次调整都发生在相同的 0.6 秒间隔内,因此锁定视图的移动速度是原来的两倍。

我假设您想将锁定视图设置为动画,因此仅将它们移动 topFrame.size.height 是不够的。这将使锁的下半部分可见。您需要移动锁定视图,使其底部边缘位于 self.view.

的顶部边缘

这意味着你需要将lockBorder.frame.origin.y设为lockBorder.frame.size.height的负数。它将移动总距离 lockBorder.frame.size.height + lockBorder.frame.origin.y,恰好等于 lockBorder.frame.maxY.

我假设 lockBorder 至少和 lockKeyhole 一样大,所以如果我们将所有视图移动 lockBorder.frame.maxY,它们都会离开屏幕。由于我们将它们移动相同的距离,因此所有视图将一起移动。

另请注意,如果您在情节提要中使用约束,那么只要您对视图层次结构执行几乎任何其他操作,这些视图就会迅速恢复到它们的原始位置。因此,您可能应该在动画结束后立即将它们从它们的超视图中移除,除非您以其他方式隐藏它们(例如通过从视图层次结构中移除它们的超视图)。

顺便说一句,标准库为你定义了M_PI

func openLock() {
    UIView.animateWithDuration(0.5, delay: 1.0, options: [], animations: {
        self.lockKeyhole.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
        }, completion: { _ in
            UIView.animateWithDuration(0.6, delay: 0.2, options: [], animations: {
                let yDelta = self.lockBorder.frame.maxY

                self.topLock.center.y -= yDelta
                self.lockKeyhole.center.y -= yDelta
                self.lockBorder.center.y -= yDelta
                self.bottomLock.center.y += yDelta
                }, completion: { _ in
                    self.topLock.removeFromSuperview()
                    self.lockKeyhole.removeFromSuperview()
                    self.lockBorder.removeFromSuperview()
                    self.bottomLock.removeFromSuperview()
            })
    })
}