gpt4 book ai didi

ios - 隐藏标签之前,UIView 动画未完成

转载 作者:行者123 更新时间:2023-11-28 12:14:19 24 4
gpt4 key购买 nike

我的标签在动画运行前就消失了。

动画应在按下按钮并滑出标签和按钮后运行。但是当我按下按钮时,在动画将它们滑出之前它们都被隐藏了。

func nextGame() {

UIView.animate(withDuration: 3, delay: 0.0, options: .allowAnimatedContent, animations: {
NSLog("Animation started")
self.labelWinningPlayer.center = CGPoint(x: self.labelWinningPlayer.center.x + 500, y: self.labelWinningPlayer.center.y)
self.buttonNextGameLabel.center = CGPoint(x: self.buttonNextGameLabel.center.x + 500, y: self.buttonNextGameLabel.center.y)
}, completion: { (finished: Bool) in
NSLog("Animation stopped")
self.labelWinningPlayer.isHidden = true
self.buttonNextGameLabel.isHidden = true
})


//sets buttons to start position
labelWinningPlayer.center = CGPoint(x: labelWinningPlayer.center.x - 1000, y: labelWinningPlayer.center.y)
buttonNextGameLabel.center = CGPoint(x: buttonNextGameLabel.center.x - 1000, y: buttonNextGameLabel.center.y)

//hides buttons that lay under the animated buttons
for i in 1...9 {

if let button = view.viewWithTag(i) as? UIButton {
button.setImage(nil, for: .normal)
}
}
activeGame = true
}

//animation should get started with this button
@IBAction func buttenNextGameAction(_ sender: Any) {
nextGame()
}

//slides buttons in
func slideNextGameButtons() {

labelWinningPlayer.isHidden = false
buttonNextGameLabel.isHidden = false

UIView.animate(withDuration: 0.5, animations: {

self.labelWinningPlayer.center = CGPoint(x: self.labelWinningPlayer.center.x + 500, y: self.labelWinningPlayer.center.y)
self.buttonNextGameLabel.center = CGPoint(x: self.buttonNextGameLabel.center.x + 500, y: self.buttonNextGameLabel.center.y)
})
}

这是 NSLog。据此,它正在运行完整的动画...

2017-11-15 23:26:17.321465 [2442:245232] Animation started
2017-11-15 23:26:20.325137 [2442:245232] Animation stopped

提前感谢您的帮助!

最佳答案

您的代码完美运行:

enter image description here

因此,无论问题是什么,您没有告诉我们的事情都是造成问题的原因。

编辑:是的,确实是您没有告诉我们的代码导致了问题。你告诉我们这件事:

UIView.animate(withDuration: 3, delay: 0.0, options: .allowAnimatedContent, animations: {
self.labelWinningPlayer.center = CGPoint(x: self.labelWinningPlayer.center.x + 500, y: self.labelWinningPlayer.center.y)
self.buttonNextGameLabel.center = CGPoint(x: self.buttonNextGameLabel.center.x + 500, y: self.buttonNextGameLabel.center.y)
}, completion: { (finished: Bool) in
self.labelWinningPlayer.isHidden = true
self.buttonNextGameLabel.isHidden = true
})


但是你没有告诉我们的内容,它们是:

labelWinningPlayer.center = CGPoint(x: labelWinningPlayer.center.x - 1000, y: labelWinningPlayer.center.y)
buttonNextGameLabel.center = CGPoint(x: buttonNextGameLabel.center.x - 1000, y: buttonNextGameLabel.center.y)


那些行取消了动画!

看来你还没有明白什么是动画。您首先制作动画,然后在动画之后将您想要完成的任何事情放入completion 函数中。但是那些行您希望动画之后完成的事情。所以将它们放入 completion 函数中!像这样:

UIView.animate(withDuration: 3, delay: 0.0, options: .allowAnimatedContent, animations: {
self.labelWinningPlayer.center = CGPoint(x: self.labelWinningPlayer.center.x + 500, y: self.labelWinningPlayer.center.y)
self.buttonNextGameLabel.center = CGPoint(x: self.buttonNextGameLabel.center.x + 500, y: self.buttonNextGameLabel.center.y)
}, completion: { (finished: Bool) in
self.labelWinningPlayer.isHidden = true
self.buttonNextGameLabel.isHidden = true
labelWinningPlayer.center = CGPoint(x: labelWinningPlayer.center.x - 1000, y: labelWinningPlayer.center.y)
buttonNextGameLabel.center = CGPoint(x: buttonNextGameLabel.center.x - 1000, y: buttonNextGameLabel.center.y)
// ... and everything else in the method goes here too
})


并且方法中的所有其他内容也需要移到那里。动画完成之后发生的所有事情都将进入completion 函数。这就是 completion 的意思!

关于ios - 隐藏标签之前,UIView 动画未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47318664/

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