iOS專案開發實戰——設定檢視的透明度改變動畫

乞力馬紮羅的雪CYF發表於2015-08-20

    在iOS中 ,透明度的改變可以讓View檢視以一種漸變的效果動態的出現或者消退,非常有意思。這裡我們將會對一個View控制元件的色塊執行透明度改變的動畫。關於其他的動畫效果可以參考我的其他幾篇部落格《iOS專案開發實戰——製作檢視的平移動畫以及解決移動異常問題》,《iOS專案開發實戰——多個檢視的平移動畫與閉包函式的宣告與呼叫》。

(1)在Main.storyboard中拖入一個View,並且繫結到程式碼中。

(2)實現程式碼如下:

import UIKit

class OpacityViewController: UIViewController {

    @IBOutlet weak var greenSquare: UIView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        

        func anim(){
        
            self.greenSquare.alpha = 0.2   //改變透明度到0.2
            
        }
        
        UIView.animateWithDuration(2, animations: anim)//時常為2s;
        
        
    }
    

}

(3)執行程式,發現色塊可以動態的改變透明度Alpha。


github主頁:https://github.com/chenyufeng1991  。歡迎大家訪問!

相關文章