ios - 无法在 Swift 中使用时间延迟为 UIImage 设置动画

标签 ios swift uiview uiimage

我试图在 3 秒后为 UIImage 的 alpha 设置动画。默认情况下,alpha 设置为 0,3 秒后,alpha 应更改为 1,从而向用户显示图像。我为动画编写的代码确实将 alpha 设置为 0,但我无法在 3 秒后将 alpha 更改为 1。我是 swift 的新手,不确定我哪里出了问题。这是我的代码。

import UIKit

class WelcomeOneViewController: UIViewController {

@IBOutlet weak var swipeImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    swipeImageView.alpha = 0
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    displaySwipeView()
}

func displaySwipeView() {
    UIView.animate(withDuration: 1.0, delay: 3.0, options: .beginFromCurrentState, animations: {

        DispatchQueue.main.async { [weak self] in
            self?.swipeImageView.alpha = 1
        }

    }, completion: nil)
}

}

最佳答案

用这段代码试试:

import UIKit

class WelcomeOneViewController: UIViewController {

    @IBOutlet weak var swipeImageView: UIImageView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        swipeImageView.alpha = 0
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        displaySwipeView()
    }

    func displaySwipeView() {
        swipeImageView.alpha = 0
        UIView.animate(withDuration: 1.0, delay: 3.0, options: [], animations: {
            self.swipeImageView.alpha = 1
        }, completion: nil)
    }

}

关于ios - 无法在 Swift 中使用时间延迟为 UIImage 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768247/

相关文章:

ios - 从一个 View Controller 到另一个 View Controller 遇到 'segueing' 问题,收到奇怪的警告

ios - 使用 Xcode 11 时,SwiftUI View 以小窗口而不是全屏呈现

swift - 在 SwiftUI 中设计自己的按钮

swift - iOS 11 拖放 UIDocument

iphone - UI 卡住并以动画方式删除 UIScrollView subview

android - 在应用程序快速关闭之前混合面板刷新事件

ios - SKScene.backgroundColor不起作用。始终为默认颜色

ios - 台风框架 : Swift or Objective-C

ios - 在初始化时向 UIView 添加 subview

ios - 如何创建 UIView 弹跳动画?