醬油08-動(dòng)畫

三種實(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")
        
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS實(shí)際開(kāi)發(fā)中常用的動(dòng)畫無(wú)非是以下四種:UIView動(dòng)畫,核心動(dòng)畫,幀動(dòng)畫,自定義轉(zhuǎn)場(chǎng)動(dòng)畫。 1.UIView...
    請(qǐng)叫我周小帥閱讀 3,334評(píng)論 1 23
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,698評(píng)論 6 30
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,273評(píng)論 5 13
  • 顯式動(dòng)畫 顯式動(dòng)畫,它能夠?qū)σ恍傩宰鲋付ǖ淖远x動(dòng)畫,或者創(chuàng)建非線性動(dòng)畫,比如沿著任意一條曲線移動(dòng)。 屬性動(dòng)畫 ...
    清風(fēng)沐沐閱讀 2,104評(píng)論 1 5
  • 前言 本文只要描述了iOS中的Core Animation(核心動(dòng)畫:隱式動(dòng)畫、顯示動(dòng)畫)、貝塞爾曲線、UIVie...
    GitHubPorter閱讀 3,753評(píng)論 7 11

友情鏈接更多精彩內(nèi)容