[iOS]UIView動畫學習筆記(上)

本文是學習慕課網UIView動畫的筆記,可用於簡單的創建動畫,
慕課網視頻鏈接如下:
http://www.imooc.com/learn/392

通過使用動畫,我們可以使我們的iOS App更加生動,提高用戶體驗和粘性。
通常,我們的動畫是在頁面剛剛顯示(viewDidAppear)或者點擊了某個按鍵(Action)的時候開始,持續一小段時間結束

使用的API是:

Void UIView.animateWithDuration(duration:NSTimeInterval, animations: () -> Void)

當然還有很多變體的方法,當我們在Xcode輸入函數名的時候,都可以展現出來,包括延遲執行和執行完成後,還可以調用另一個closure

我們的比較初級的動畫效果分爲如下幾類:
- 位置
- 透明度
- 大小
- 顏色
- 翻轉

下面我們依次進行說明:

位置

override func viewDidAppear(animated: Bool){
    super.viewDidAppear(animated)
    UIView.animateWithDuration(1, animations:{
        self.blueSquare.center.x = self.view.bounds.width - self.blueSquare.center.x
    })
    UIView.animateWithDuration(1, delay:0.5, options: nil, animations:{
        self.redSquare.center.y = self.view.bounds.height - self.redSquare.center.y
    }, completion: nil)
    UIView.animateWithDuration(1, delay:0.5, options: nil, animations:{
        self.greenSquare.center.x = self.view.bounds.width - self.greenSquare.center.x
        self.greenSquare.center.y = self.view.bounds.height - self.greenSquare.center.y
    }, completion: nil)

}

透明度(Opacity)

override func viewDidAppear(animated: Bool){
    super.viewDidAppear(animated)
    UIView.animateWithDuration(1, animations:{
        self.blueSquare.alpha = 0.1
    })  
}

大小

override func viewDidAppear(animated: Bool){
    super.viewDidAppear(animated)
    UIView.animateWithDuration(1, animations:{
        self.blueSquare.transform = CGAffineTransformMakeScale(2.0,2.0)
    })  
}

顏色

override func viewDidAppear(animated: Bool){
    super.viewDidAppear(animated)
    UIView.animateWithDuration(1, animations:{
        self.blueSquare.backgroundColor = UIColor.redColor()
        self.label.textColor = UIColor.whiteColor()
    })  
}

翻轉

override func viewDidAppear(animated: Bool){
    super.viewDidAppear(animated)
    UIView.animateWithDuration(1, animations:{
        self.blueSqure.transform = CGAffineTransformRotation(CGFloat(M_PI))
    })  
}
發佈了31 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章