View Animations
Animatable properties
? bounds: 改變 bounds 屬性可以在當(dāng)前 view 內(nèi)改變子視圖等的相對(duì)位置腋腮。
? frame: 改變 frame 可以移動(dòng)或者縮放 view 忽孽。
? center: 當(dāng)你想移動(dòng) view 到屏幕的新位置時(shí)膛堤,可以改變此屬性锹锰。
? backgroundColor: 背景顏色。
? alpha: 改變此屬性童芹,可以實(shí)現(xiàn)淡入淡出效果恶座。
Animation options
options: 修改該屬性,可以實(shí)現(xiàn)多個(gè)自定義動(dòng)畫(huà)效果蚂四。
Repeating:
? .repeat: 添加該屬性光戈,可以使動(dòng)畫(huà)一直循環(huán)重復(fù)執(zhí)行。
? .autoreverse: 只能和 .repeat 結(jié)合使用遂赠,先向前執(zhí)行動(dòng)畫(huà)田度,然后向相反方向進(jìn)行動(dòng)畫(huà)。
eg.
UIView.animate(withDuration: 0.5, delay: 0.4,
options: [.repeat, .autoreverse], animations: {
self.password.center.x += self.view.bounds.width
},
completion: nil )
Animation easing
現(xiàn)實(shí)中解愤,物體移動(dòng)不是突然開(kāi)始與戛然而止的,像汽車(chē)和火車(chē)那樣的運(yùn)動(dòng)乎莉,是比較優(yōu)雅的加速送讲、高速運(yùn)行、減速過(guò)程惋啃,像這樣:
為使我們的動(dòng)畫(huà)看起來(lái)更接近現(xiàn)實(shí)哼鬓,我們可以通過(guò)簡(jiǎn)單設(shè)置 Animation easing 實(shí)現(xiàn)。
? .curveLinear: 該選項(xiàng)使動(dòng)畫(huà)無(wú)加速亦無(wú)減速過(guò)程边灭。
? .curveEaseIn: 該選項(xiàng)使動(dòng)畫(huà)在開(kāi)始的時(shí)候有個(gè)加速過(guò)程异希。
? .curveEaseOut: 該選項(xiàng)使動(dòng)畫(huà)在結(jié)束的時(shí)候有個(gè)減速過(guò)程。
? .curveEaseInOut: 該選項(xiàng)使動(dòng)畫(huà)在開(kāi)始的時(shí)候有個(gè)加速過(guò)程绒瘦,在結(jié)束的時(shí)候有個(gè)減速過(guò)程称簿。
eg.
UIView.animate(withDuration: 0.5, delay: 0.4,
options: [.repeat, .autoreverse, .curveEaseIn],
animations: {
self.password.center.x += self.view.bounds.width
},
completion: nil )
附:demo下載地址