介紹下iOS開(kāi)發(fā)動(dòng)畫的幾種方式吧:
一. 代碼放在Begin和Commit之間:
[UIView beginAnimations:nil context:nil]; // 開(kāi)始動(dòng)畫
// Code...
[UIView commitAnimations]; // 提交動(dòng)畫
e.g.
[UIView beginAnimations:nil context:nil]; // 開(kāi)始動(dòng)畫
[UIView setAnimationDuration:10.0]; // 動(dòng)畫時(shí)長(zhǎng)
/**
* 圖像向下移動(dòng)
*/
CGPoint point = _imageView.center;
point.y += 150;
[_imageView setCenter:point];
[UIView commitAnimations]; // 提交動(dòng)畫
但這一方式好像是有些過(guò)時(shí)了梅猿,API文檔最后面是這樣寫的:
Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.
二. Block
UIView
類幾個(gè)類方法用于實(shí)現(xiàn)相對(duì)簡(jiǎn)單的動(dòng)畫
e.g.
//準(zhǔn)備工作
lazy var box:UIView = UIView()
.
.
.
self.view.addSubview(box)
box.backgroundColor = UIColor.red
box.snp.makeConstraints { (make) -> Void in
make.width.height.equalTo(50)
make.centerY.equalTo(self.view)
make.left.equalTo(self.view.snp.left)
}
1. 基本動(dòng)畫
UIView.animate(withDuration: 3) {
self.box.frame.origin.x = self.view.frame.width - 50
}
2. 添加結(jié)束處理
UIView.animate(withDuration: 3, animations: {
self.box.frame.origin.x = self.view.frame.width - 50
}) { (isFinished) in
if isFinished {
UIView.animate(withDuration: 3, animations: {
self.box.frame.origin.x = 0
})
}
}
3. 添加動(dòng)畫配置
UIView.animate(withDuration: 3, delay: 0.0, options: [.repeat,.autoreverse], animations: {
self.box.frame.origin.x = self.view.frame.width - 50
}) { (isFinished) in
}
4. Spring動(dòng)畫
UIView.animate(withDuration: 3, delay: 1, usingSpringWithDamping: 1.0, initialSpringVelocity: 5.0, options: .curveEaseIn, animations: {
self.box.frame.origin.x = self.view.frame.width - 50
}) { (isFinished) in
}
5. 關(guān)鍵幀動(dòng)畫
func doKeyFrameworkAnimation() {
UIView.animateKeyframes(withDuration: 4.0, delay: 0.0, options: [.calculationModeCubic, .calculationModeLinear], animations: {
let colors:[UIColor] = [UIColor.black,UIColor.red,UIColor.blue,UIColor.yellow]
let frames:[CGRect] = [CGRect.init(x: 0, y: 0, width: 20, height: 20),
CGRect.init(x: 50, y: 50, width: 30, height: 30),
CGRect.init(x: 100, y: 100, width: 40, height: 40),
CGRect.init(x: 150, y: 150, width: 50, height: 50)]
for index in 0 ..< colors.count {
UIView.addKeyframe(withRelativeStartTime: Double(index)/Double(colors.count), relativeDuration: Double(1)/Double(colors.count), animations: {
self.box.backgroundColor = colors[index]
self.box.frame = frames[index]
})
}
}) { (isFinished) in
}
}
以上各方法說(shuō)明與參數(shù)說(shuō)明請(qǐng)參看文檔。不復(fù)制撕阎,不翻譯了??
三. Core Foundation(略)
四. Lottie
Lottie Git
使用這個(gè)庫(kù)以后,很多動(dòng)畫都讓UED去做了伸头。終于刚陡,開(kāi)發(fā)不用再寫動(dòng)畫了~!碧聪???