如标题所述,我无法让UIButton淡入ViewDidLoad方法。这是我到目前为止的代码:
ViewController.swift
import UIKit
import QuartzCore
class ViewController: UIViewController {
@IBOutlet weak var nextButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.nextButton.fadeIn(duration: 10.0, delay: 10.0)
}
}
UIViewExtensions.swift
import Foundation
import UIKit
extension UIView {
func fadeIn(duration: NSTimeInterval = 1.0, 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 = 1.0, 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)
}
}
答案 0 :(得分:1)
您不希望在viewDidLoad上启动任何淡入效果。当类初始化(在init之后)时调用此方法。它在视图可见之前发生。您想在viewDidAppear中启动动画。一旦视图在屏幕上可见,就会调用此方法。当你在viewDidLoad中启动它时,它已经在到达viewDidAppear时完成了动画,假设当然你在大约0.5秒内完成了淡入淡出。
答案 1 :(得分:1)
在故事板上将按钮alpha设置为0,以便您可以淡入。
因为,如果你的按钮alpha是1,那么你想要动画为1,IOS什么都不做
override func viewDidAppear(animated: Bool) {
self.button.fadeIn(duration: 3, delay: 0)
}