【问题标题】:Swift Animation Enclosure Won't AnimateSwift Animation Enclosure 不会设置动画
【发布时间】:2015-07-24 02:21:21
【问题描述】:

我正在 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 动画与第二个相同?

提前感谢所有回复的人。

【问题讨论】:

    标签: ios swift animation uiview ibaction


    【解决方案1】:

    请改用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)
                })
        }
    

    【讨论】:

    • 当我尝试使用您在我的程序的不同区域中创建的第一个示例时,对象会迅速回到它们的原始位置。是否有一个原因?我没有使用自动布局...
    • 我的猜测是您的第三个关键帧动画的 relativeDuration(将标签从右侧移回屏幕)可能太短而无法注意到。关键帧动画的工作方式是,每个动画将在您设置的相对持续时间的相对开始时间执行。因此,在这种情况下它并没有很好地工作。由于隐藏和移动的事情是同时发生的,你可以看到标签飞到右边消失了。因此,我建议您使用第二个示例或放置另一个关键帧动画只是为了隐藏。 (但这会很混乱,请参阅我编辑的答案)。
    • 对于弹回原位问题,尝试增加从右移回屏幕动画的相对时长。
    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多