Swift 动画外壳不会动画

Swift Animation Enclosure Won't Animate

我正在 Swift 中创建一个不使用 AutoLayout 的 iPad 应用程序。我的 viewDidLoad 方法是空的,我将我的动画代码放在我的 IBAction 中,如下所示:

@IBAction func nextStep(sender: AnyObject) {
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)
        })
        comparisonLabel.text = "Speed Reading"
        comparisonLabel.center = CGPointMake(comparisonLabel.center.x + 800, comparisonLabel.center.y)
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)
        })
}

第一个 UIView 附件没有为标签设置动画。相反,它只是立即从屏幕上删除标签。出于某种原因,第二个 UIView 完全正常工作,从屏幕右侧逐渐滑动标签。 (我没有使用 viewDidAppear 函数,因为我只需要在用户单击按钮时发生动画。)

有没有人碰巧知道为什么会这样,以及如何让第一个 UIView 动画与第二个动画执行相同?

提前感谢所有回复的人。

改用UIView.animateKeyframesWithDuration

例子

UIView.animateKeyframesWithDuration(2, delay: 0, options: nil, animations: { () -> Void in

        UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.5, animations: {

            //Move left animation
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)


        })

        UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.1, animations: {

            //Hiding animation
            self.comparisonLabel.alpha = 0

        })

        UIView.addKeyframeWithRelativeStartTime(0.6, relativeDuration: 0.1, animations: {

            //Move to right animation
            self.comparisonLabel.text = "Speed Reading"
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x + 800, self.comparisonLabel.center.y)

        })

        UIView.addKeyframeWithRelativeStartTime(0.6, relativeDuration: 0.4, animations: {

            //Moving back into screen animation
            self.comparisonLabel.alpha = 1
            self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)

        })

        }, completion: nil)

尝试使用 relativestarttime 和 relativeduration 来获得你想要的效果。

或使用

class func animateWithDuration(_ duration: NSTimeInterval,
                animations animations: () -> Void,
                completion completion: ((Bool) -> Void)?)

例子

UIView.animateWithDuration(1, animations: { () -> Void in

        //Your first animation
        self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)

        }) { (finish:Bool) -> Void in

            self.comparisonLabel.text = "Speed Reading"
            self.comparisonLabel.center = CGPointMake(comparisonLabel.center.x + 800, comparisonLabel.center.y)

            UIView.animateWithDuration(1, animations: { () -> Void in
                //Your second animation
                self.comparisonLabel.center = CGPointMake(self.comparisonLabel.center.x - 400, self.comparisonLabel.center.y)
            })
    }