在iOS中,實現(xiàn)動畫有兩種方法哩都。一個是統(tǒng)一的animateWithDuration瞎颗,另一個是組合出現(xiàn)的beginAnimations和commitAnimations月洛。這三個方法都是類方法何恶。
使用animateWithDuration來實現(xiàn)動畫
此方法共有5個參數(shù):
- duration:動畫從開始到結(jié)束的持續(xù)時間,單位是秒
- delay:動畫開始前等待的時間
- options:動畫執(zhí)行的選項嚼黔。里面可以設(shè)置動畫的效果细层∠Ъ可以使用UIViewAnimationOptions類提供的各種預(yù)置效果
- anmations:動畫效果的代碼塊
- completion:動畫執(zhí)行完畢后執(zhí)行的代碼塊
UIView支持動畫效果的屬性
- frame:此屬性包含一個矩形,即邊框矩形疫赎,此值確定了當(dāng)前視圖在其父視圖坐標(biāo)系中的位置與尺寸
- bounds:也是矩形盛撑,邊界矩形,它指的是視圖在其自己的坐標(biāo)系中的位置和尺寸捧搞,左上角坐標(biāo)永遠(yuǎn)是(0,0)
- center:確定視圖的中心點在其父視圖坐標(biāo)系中的位置坐標(biāo)抵卫。即定義當(dāng)前視圖在父視圖中的位置
- alpha:視圖的透明度。(但視圖完全透明時胎撇,不能響應(yīng)觸摸消息)
- backgroundColor:背景色
- transform:這是一種3×3的變化矩陣介粘。通過這個矩陣我們可以對一個坐標(biāo)系統(tǒng)進(jìn)行縮放、平移晚树、旋轉(zhuǎn)以及這兩者的任意組操作姻采。
Transform(變化矩陣)的四個常用的變換方法
- CGAffineTransformMake():返回變換矩陣
- CGAffineTransformMakeTranslation():返回平移變換矩陣
- CGAffineTransformMakeScale():返回縮放變換矩陣
- CGAffineTransformMakeRotation():返回旋轉(zhuǎn)變換矩陣
樣例1:方塊初始縮小為原始尺寸1/10。在1秒的動畫中復(fù)原到完整大小题涨,同時還伴隨旋轉(zhuǎn)效果偎谁。
import UIKit
class ViewController: UIViewController {
var dimension:Int = 4 // 游戲方格維度
var width:CGFloat = 50 // 數(shù)字格子的寬度
var padding:CGFloat = 6 // 格子與格子的間距
var backgrounds:Array<UIView>! // 保存背景圖數(shù)據(jù)
override func viewDidLoad() {
super.viewDidLoad()
self.backgrounds = Array<UIView>()
setupGameMap()
playAnimation()
}
func setupGameMap() {
var x:CGFloat = 50
var y:CGFloat = 150
for i in 0..<dimension {
print(i)
y = 150
for _ in 0..<dimension {
// 初始化視圖
let background = UIView(frame:CGRectMake(x, y, width, width))
background.backgroundColor = UIColor.darkGrayColor()
self.view.addSubview(background)
// 將視圖保存起來总滩,以備后用
backgrounds.append(background)
y += padding + width
}
x += padding+width
}
}
func playAnimation() {
for tile in backgrounds {
// 先將數(shù)字塊大小置為原始尺寸的 1/10
tile.layer.setAffineTransform(CGAffineTransformMakeScale(0.1,0.1))
// 設(shè)置動畫效果,動畫時間長度1 秒.
UIView.animateWithDuration(1, delay:0.01,
options:UIViewAnimationOptions.TransitionNone, animations:
{
()-> Void in
// 在動畫中,數(shù)字塊有一個角度的旋轉(zhuǎn)
tile.layer.setAffineTransform(CGAffineTransformMakeRotation(90))
},
completion:{
(finished:Bool) -> Void in
UIView.animateWithDuration(1, animations:{
()-> Void in
// 完成動畫時,數(shù)字塊復(fù)原
tile.layer.setAffineTransform(CGAffineTransformIdentity)
})
})
}
}
}
樣例2:只有從小變大的效果
func playAnimation() {
for tile in backgrounds{
// 先將數(shù)字塊大小置為原始尺寸的1/10
tile.layer.setAffineTransform(CGAffineTransformMakeScale(0.1,0.1))
// 設(shè)置動畫效果,動畫時間長度 1 秒.
UIView.animateWithDuration(1, delay:0.01,
options:UIViewAnimationOptions.TransitionNone, animations:
{
()-> Void in
tile.layer.setAffineTransform(CGAffineTransformMakeScale(1,1))
},
completion:{
(finished:Bool) -> Void in
UIView.animateWithDuration(0.08, animations:{
()-> Void in
tile.layer.setAffineTransform(CGAffineTransformIdentity)
})
})
}
}
樣例3:方塊從不透明到透明的效果
func playAnimation() {
for tile in backgrounds{
tile.alpha = 0;
// 設(shè)置動畫效果,動畫時間長度 1 秒纲堵。
UIView.animateWithDuration(1, delay:0.01,
options:UIViewAnimationOptions.CurveEaseInOut, animations:
{
()-> Void in
},
completion:{
(finished:Bool) -> Void in
UIView.animateWithDuration(1, animations:{
()-> Void in
tile.alpha = 1
})
})
}
}