简体   繁体   English

Swift:animateWithDuration行为

[英]Swift: animateWithDuration behavior

I have made simple animation of text and buttons to came from out of the screen to middle of the screen, but it doesn't. 我已经制作了简单的文本和按钮动画,可以从屏幕外移动到屏幕中间,但事实并非如此。 It goes from screen, to out of the screen. 它从屏幕转到屏幕外。 Basically, animation is reversed. 基本上,动画是相反的。

I have seen that other apps works the way I wont with same code, so I do not know what is wrong. 我已经看到其他应用程序可以用相同的代码来工作,所以我不知道出什么问题了。 :( :(

Here is the code: 这是代码:

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)
}
}

Information about where the views started would be required to debug this. 要调试此视图,将需要有关视图从何处开始的信息。 However, I would strongly recommend against using += or -= for animations like this. 但是,我强烈建议不要对此类动画使用+ =或-=。 As you're experiencing, it's behavior is unpredictable since it depends entirely on the current state. 在您体验时,它的行为是不可预测的,因为它完全取决于当前状态。

Instead, you could do this in the viewWillAppear: *view*.center.x = -(*view*.frame.width / 2) and in viewDidAppear: view .center.x = whatever value you need here 相反,您可以在viewWillAppear中执行此操作: *view*.center.x = -(*view*.frame.width / 2)在viewDidAppear中执行: view .center.x = 此处需要的任何值

Also, the more up to date approach for all of this is to use and animate constraints, rather than frames. 同样,所有这些的最新方法是使用约束并设置约束动画,而不是框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM