三種實(shí)現(xiàn)動(dòng)畫效果的方法
1. CALayer(CATransaction)
import UIKit
class ViewController: UIViewController {
var redview : redView!
var layer : CALayer!
var shapeLayer: CAShapeLayer!
override func viewDidLoad() {
super.viewDidLoad()
layer = CALayer()
layer.frame = CGRect(x: 200, y: 100, width: 100, height: 100)
layer.backgroundColor = UIColor.brownColor().CGColor
self.view.layer.addSublayer(layer)
//CALayer中自帶繪圖功能
// shapeLayer = CAShapeLayer()
// shapeLayer.frame = self.view.bounds
// shapeLayer.strokeColor = UIColor.blackColor().CGColor
// shapeLayer.fillColor = UIColor.clearColor().CGColor
// self.view.layer.addSublayer(shapeLayer)
//
}
1.1 點(diǎn)擊按鈕方法
@IBAction func Animation(sender: UIButton) {
//開(kāi)始設(shè)置CALayer動(dòng)畫的屬性
CATransaction.begin()
//設(shè)置動(dòng)畫的運(yùn)行時(shí)間
CATransaction.setAnimationDuration(5)
//設(shè)置動(dòng)畫出現(xiàn)的效果(kCAMediaTimingFunctionEaseIn)
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn))
//設(shè)置動(dòng)畫所需要的效果
layer.frame = CGRect(x: 300, y: 200, width: 50, height: 50)
layer.backgroundColor = UIColor.redColor().CGColor
//開(kāi)始執(zhí)行動(dòng)畫
CATransaction.commit()
}
2. UIView中自帶的方法(animateWithDuration)
import UIKit
class ViewController: UIViewController {
var redview : redView!
var layer : CALayer!
var shapeLayer: CAShapeLayer!
override func viewDidLoad() {
super.viewDidLoad()
redview = redView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
redview.backgroundColor = UIColor.redColor()
self.view.addSubview(redview)
}
2.1 點(diǎn)擊按鈕方法
@IBAction func Animation(sender: UIButton) {
//animateWithDuration方法
UIView.animateWithDuration(5) {
self.redview.backgroundColor = UIColor.brownColor()
self.redview.frame = CGRect(x: 200, y: 200, width: 100, height: 100)
}
//兩種不同的方法
UIView.animateWithDuration(5, delay: 0, options: .AllowUserInteraction, animations: {
self.redview.backgroundColor = UIColor.yellowColor()
}) { (true) in
print("ok")
}
//設(shè)置UIView的動(dòng)畫屬性,與animateWithDuration不同,與CALayer相似
UIView.beginAnimations("first", context: nil)
UIView.setAnimationDuration(1)
//設(shè)置重復(fù)次數(shù)
UIView.setAnimationRepeatCount(100)
//設(shè)置回到起始位置
UIView.setAnimationRepeatAutoreverses(true)
// UIView.setAnimationDelay(2)
redview.frame = CGRect(x: 200, y: 200, width: 50, height: 50)
redview.backgroundColor = UIColor.brownColor()
UIView.commitAnimations()
}
3. CALayer(CABasicAnimation/CAKeyframeAnimation/CAAnimationGroup)
import UIKit
class ViewController: UIViewController {
var redview : redView!
var layer : CALayer!
var shapeLayer: CAShapeLayer!
override func viewDidLoad() {
super.viewDidLoad()
layer = CALayer()
layer.frame = CGRect(x: 200, y: 100, width: 100, height: 100)
layer.backgroundColor = UIColor.brownColor().CGColor
self.view.layer.addSublayer(layer)
}
3.1 點(diǎn)擊按鈕方法
@IBAction func Animation(sender: UIButton) {
//設(shè)置動(dòng)畫移動(dòng)路徑為中心移動(dòng)
let ani01 = CABasicAnimation(keyPath: "position")
//設(shè)置繪圖大小及路徑
let path = CGPathCreateMutable()
CGPathAddRect(path, nil, CGRect(x: 150, y: 200, width: 200, height: 100))
shapeLayer.path = path
//設(shè)置動(dòng)畫起點(diǎn)/偏移/終點(diǎn)(有兩個(gè)即可,右下為正)
ani01.fromValue = NSValue(CGPoint: CGPoint(x: 100, y: 100))
ani01.byValue = NSValue(CGPoint: CGPoint(x: 200, y: 100))
ani01.toValue = NSValue(CGPoint: CGPoint(x: 300, y: 300))
ani01.duration = 5
layer.addAnimation(ani01, forKey: "ani01")
//設(shè)置動(dòng)畫移動(dòng)時(shí)顏色變化
let ani02 = CABasicAnimation(keyPath: "backgroundColor")
ani02.fromValue = UIColor.blueColor().CGColor
ani02.toValue = UIColor.redColor().CGColor
//設(shè)置動(dòng)畫在路徑上移動(dòng)時(shí)顏色變化
let ani03 = CAKeyframeAnimation(keyPath: "position")
ani03.path = path
//設(shè)置變化的顏色
ani03.values = [
UIColor.redColor().CGColor,
UIColor.blackColor().CGColor,
UIColor.greenColor().CGColor
]
//設(shè)置變化時(shí)間
ani03.keyTimes = [0,0.5,0.7]
ani02.duration = 5
layer.addAnimation(ani02, forKey: "ani02")
//設(shè)置動(dòng)畫組(實(shí)現(xiàn)多種動(dòng)畫)
let anis = CAAnimationGroup()
anis.animations = [ani01, ani02, ani03]
anis.duration = 5
layer.addAnimation(anis, forKey: "anis")
}