簡體   English   中英

UIView.animateWithDuration不對單獨的視圖控制器進行動畫處理

[英]UIView.animateWithDuration doesn't animate on separate view controller

將動畫設置為反復淡入和淡出。 “ viewSmall”和“ viewBig”是UIImageViews。

查看情節提要鏡頭

class TestViewController: UIViewController {

    @IBOutlet weak var viewBig: UIImageView!
    @IBOutlet weak var viewSmall: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        animate()
    }

    func animate() {
        UIView.animateWithDuration(0.5,
                                   delay: 0,
                                   options: [.Repeat, .Autoreverse],
                                   animations: {
                                    self.viewSmall.alpha = 0
            },
                                   completion: nil)

        UIView.animateWithDuration(0.5,
                                   delay: 0,
                                   options: [.Repeat, .Autoreverse],
                                   animations: {
                                    self.viewBig.alpha = 0
            },
                                   completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

這段代碼不會在僅有的幾個視圖控制器上進行動畫處理,而且我認為這些視圖控制器似乎是獨立的,與其他視圖控制器無關。 因為此代碼在連接到導航視圖控制器的視圖控制器上可以完美工作。

如何在獨立的視圖控制器上制作它? 請幫忙。

找到了一個絕妙的解決方案。 使用dispatch_after捕獲持續時間和延遲。

func animate(){
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {

        // Animate the transition
        UIView.animateWithDuration(0.5,
                               delay: 0,
                               options: [.Repeat, .Autoreverse],
                               animations: {
                                self.viewSmall.alpha = 0
        },
                               completion: nil)

        UIView.animateWithDuration(0.5,
                               delay: 0,
                               options: [.Repeat, .Autoreverse],
                               animations: {
                                self.viewBig.alpha = 0
        },
                               completion: nil)
    })
}

來源: Swift UIView animateWithDuration完成關閉立即調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM