gpt4 book ai didi

ios - Swift:animateWithDuration 行为

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

我制作了简单的文本和按钮动画,从屏幕外到屏幕中间,但它没有。它从屏幕上移到屏幕外。基本上,动画是反转的。

我已经看到其他应用程序使用相同的代码以我惯用的方式工作,所以我不知道哪里出了问题。 :(

代码如下:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var welcomeText: UILabel!
@IBOutlet weak var worksText: UILabel!
@IBOutlet weak var loginButton: UIButton!
@IBOutlet weak var signUp: UIButton!
@IBOutlet weak var socialLog: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

}

override func viewWillAppear(animated: Bool) {

welcomeText.alpha = 0.0
worksText.alpha = 0.0
loginButton.alpha = 0.0
signUp.alpha = 0.0
socialLog.alpha = 0.0

welcomeText.center.x -= view.bounds.width
worksText.center.x -= view.bounds.width
socialLog.center.x -= view.bounds.width
signUp.center.x -= view.bounds.width
}

override func viewDidAppear(animated: Bool) {

self.welcomeText.fadeIn()
UIView.animateWithDuration(0.7, delay: 0.1, options: .CurveEaseOut, animations: {
self.welcomeText.center.x += self.view.bounds.width
self.view.layoutIfNeeded()
}, completion: nil)

self.worksText.fadeIn()
UIView.animateWithDuration(0.7, delay: 0.5, options: .CurveEaseOut, animations: {
self.worksText.center.x += self.view.bounds.width
self.view.layoutIfNeeded()
}, completion: nil)

self.socialLog.fadeIn()
UIView.animateWithDuration(0.7, delay: 0.3, options: .CurveEaseOut, animations: {
self.socialLog.center.x += self.view.bounds.width
self.view.layoutIfNeeded()
}, completion: nil)

self.signUp.fadeIn()
UIView.animateWithDuration(0.7, delay: 0.7, options: .CurveEaseOut, animations: {
self.signUp.center.x += self.view.bounds.width
self.signUp.alpha = 0.5
self.view.layoutIfNeeded()
}, completion: nil)
}
}

extension UIView {
func fadeIn(duration: NSTimeInterval = 0.5, delay: NSTimeInterval = 0.0, completion: ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion) }

func fadeOut(duration: NSTimeInterval = 0.5, delay: NSTimeInterval = 0.0, completion: (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.alpha = 0.0
}, completion: completion)
}
}

最佳答案

调试时需要有关 View 开始位置的信息。但是,我强烈建议不要对这样的动画使用 += 或 -= 。正如您所体验的那样,它的行为是不可预测的,因为它完全取决于当前状态。

相反,您可以在 viewWillAppear 中执行此操作:*view*.center.x = -(*view*.frame.width/2)在 viewDidAppear 中:view.center.x = 你在这里需要的任何值

此外,所有这些的最新方法是使用和动画化约束,而不是框架。

关于ios - Swift:animateWithDuration 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38775696/

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