設(shè)置UIView的屬性:
- Position 位置
- Opacity 透明度
- Scale 比例
- Color 顏色
設(shè)置變化:
- Rotation 旋轉(zhuǎn)
- Repeat 重復(fù)
- Easing 曲線
- spring 彈簧
動(dòng)畫(huà)方法
- 在一定時(shí)間內(nèi) View 的屬性發(fā)生改變,動(dòng)畫(huà)結(jié)束后回調(diào)坤溃。
classfunc animateWithDuration(duration:NSTimeInterval, animations: () ->Void, completion: ((Bool) ->Void)?)
- 在一定時(shí)間內(nèi) View 的屬性發(fā)生改變两入。無(wú)回調(diào)。
classfunc animateWithDuration(duration:NSTimeInterval, animations: () ->Void)
- 可以設(shè)置動(dòng)畫(huà)執(zhí)行的方式
classfunc animateWithDuration(duration:NSTimeInterval, delay:NSTimeInterval, options:UIViewAnimationOptions, animations: () ->Void, completion: ((Bool) ->Void)?)
- 參數(shù)說(shuō)明:
- duration : 動(dòng)畫(huà)經(jīng)歷時(shí)長(zhǎng)
- delay : 延遲時(shí)間,在該延遲時(shí)間后才執(zhí)行動(dòng)畫(huà)
- options : 系統(tǒng)提供了許多動(dòng)畫(huà)執(zhí)行的方式,比如以下幾個(gè)
- Repeat : UIViewAnimationOptions 重復(fù)
- CurveEaseInOut : UIViewAnimationOptions 由慢到快再到慢
- CurveEaseIn : UIViewAnimationOptions 由慢到快
- CurveEaseOut : UIViewAnimationOptions 由快到慢
- CurveLinear : UIViewAnimationOptions 勻速
- animation : UIView動(dòng)畫(huà)結(jié)束時(shí)的狀態(tài) ( 比如 : UIView移動(dòng)到另一點(diǎn),變成某一種顏色,放大(縮小)后的比例,變化到某一透明度,視圖旋轉(zhuǎn)到某一角度)
- completion : 動(dòng)畫(huà)結(jié)束時(shí)的回調(diào)(這里可以處理一些事件)
- 可設(shè)置彈跳效果的動(dòng)畫(huà)
classfunc animateWithDuration(duration:NSTimeInterval, delay:NSTimeInterval, usingSpringWithDamping dampingRatio:CGFloat, initialSpringVelocity velocity:CGFloat, options:UIViewAnimationOptions, animations: () ->Void, completion: ((Bool) ->Void)?)
* 參數(shù)說(shuō)明:
* usingSpringWithDamping : 阻尼(彈性系數(shù))
* initialSpringVelocity : 初始速率
小示例如下:
1.Position 位置
UIView.animate(withDuration: 1, animations:{
self.greenView.center.x = self.view.bounds.width - self.greenView.center.x
self.greenView.center.y = self.view.bounds.height - self.greenView.center.y
})
UIView.animate(withDuration: 1, delay: 0.5, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.redView.center.x = self.view.bounds.width - self.redView.center.x
self.redView.center.y = self.redView.center.y - self.view.bounds.height
}, completion: nil)
2.Opacity 透明度
UIView.animate(withDuration: 1, animations: {
self.OpacityView.alpha = 0.1
})
3.Scale 比例
UIView.animate(withDuration: 1, animations: {
self.scaleView.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
})
4.Color 顏色
UIView.animate(withDuration: 1, animations: {
self.backgroundView.backgroundColor = UIColor.black
self.textLabel.textColor = UIColor.yellow
})
5.Rotation 旋轉(zhuǎn)
UIView.animate(withDuration: 1, animations: {
self.iamgeView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))
})
6.Repeat 重復(fù)
UIView.animate(withDuration: 1, animations: {
self.blueView.center.x = self.view.bounds.width - self.blueView.center.x
})
UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.repeat, animations: {
self.redView.center.x = self.view.bounds.width - self.redView.center.x
}, completion: nil)
UIView.animate(withDuration: 1, delay: 0, options: [UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: {
self.greenView.center.x = self.view.bounds.width - self.greenView.center.x
}, completion: nil)
7.Easing 曲線
UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
self.blueView.center.x = self.view.bounds.width - self.blueView.center.x
}, completion: nil)
UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.redView.center.x = self.view.bounds.width - self.redView.center.x
}, completion: nil)
UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.greenView.center.x = self.view.bounds.width - self.greenView.center.x
}, completion: nil)
UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.yellowView.center.x = self.view.bounds.width - self.yellowView.center.x
}, completion: nil)
8.spring 彈簧
UIView.animate(withDuration: 1, animations: {
self.blueView.center.x = self.view.bounds.width - self.blueView.center.x
})
UIView.animate(withDuration: 3, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: UIViewAnimationOptions.curveLinear, animations: {
self.redView.center.x = self.view.bounds.width - self.redView.center.x
}, completion: nil)
UIView.animate(withDuration: 3, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 1, options: UIViewAnimationOptions.curveLinear, animations: {
self.greenView.center.x = self.view.bounds.width - self.greenView.center.x
}, completion: nil)