gpt4 book ai didi

ios - Swift 动画外壳不会动画

转载 作者:行者123 更新时间:2023-11-29 01:49:07 26 4
gpt4 key购买 nike

我正在使用 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)
})
}

关于ios - Swift 动画外壳不会动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31601168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com