最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

swift實(shí)現(xiàn)顏色漸變以及轉(zhuǎn)換動畫

 更新時間:2022年01月26日 11:07:56   作者:LinShunIos  
這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)顏色漸變以及轉(zhuǎn)換動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文是通過結(jié)合使用CAGradientLayer、CABasicAnimation以及CAAnimationDelegate來達(dá)到顏色漸變以及轉(zhuǎn)換的動畫,下面是今天要達(dá)成的效果圖:

首先創(chuàng)建一個CAGradientLayer和幾個自己喜歡的顏色,讓VC持有。

let colorOne = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1).cgColor
let colorTwo = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1).cgColor
let colorThree = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1).cgColor
let gradient = CAGradientLayer()

接下來為gradient賦值,將其frame等同于視圖的大小,然后顏色先設(shè)置為colorOne和colorTwo,起始點(diǎn)和結(jié)束點(diǎn)分別為CGPoint(x:0, y:0)和CGPoint(x:1, y:1),并設(shè)置讓其在后臺線程異步繪制,最后添加到view的layer的sublayer中。

gradient.frame = self.view.bounds
gradient.colors = [colorOne,colorTwo]
gradient.startPoint = CGPoint(x:0, y:0)
gradient.endPoint = CGPoint(x:1, y:1)
gradient.drawsAsynchronously = true
self.view.layer.insertSublayer(gradient, at: 0)

現(xiàn)在運(yùn)行后會得到下面的結(jié)果:

顏色漸變是做到了,那么如何做到顏色漸變的轉(zhuǎn)換呢?這里還是需要用到CABasicAnimation.
在gradient創(chuàng)建完之后,添加并調(diào)用一個方法animateGradient,在里面添加一個keyPath為colors的CABasicAnimation,設(shè)置動畫時長為3s,設(shè)置結(jié)束值等一系列屬性。

func animateGradient() {
? let gradientChangeAnimation = CABasicAnimation(keyPath: "colors")
? ? ? ? gradientChangeAnimation.duration = 3.0
? ? ? ? gradientChangeAnimation.toValue = ?[colorTwo,colorThree]
? ? ? ? gradientChangeAnimation.fillMode = CAMediaTimingFillMode.forwards
? ? ? ? gradientChangeAnimation.isRemovedOnCompletion = false
? ? ? ? gradient.add(gradientChangeAnimation, forKey: "gradientChangeAnimation")
? ? ? }

這里就完成了轉(zhuǎn)換動畫。但是這里有個問題就是這里只轉(zhuǎn)換了一次,無法轉(zhuǎn)換多次顏色。那么這里就需要設(shè)置好toValue,讓每次的toValue都不一樣。
創(chuàng)建一個currentGradient和gradientSet讓VC持有。

var currentGradient: Int = 0
var gradientSet = [[CGColor]]()

在animateGradient中每次調(diào)用的時候,都對currentGradient的值進(jìn)行判斷和處理。

if currentGradient < gradientSet.count - 1 {
? ? ? ? ? ? currentGradient += 1
? ? ? ? } else {
? ? ? ? ? ? currentGradient = 0
? ? ? ? }

并修改gradientChangeAnimation的toValue:

let gradientChangeAnimation = CABasicAnimation(keyPath: "colors")
gradientChangeAnimation.duration = 3.0
gradientChangeAnimation.toValue = gradientSet[currentGradient]
gradientChangeAnimation.fillMode = CAMediaTimingFillMode.forwards
gradientChangeAnimation.isRemovedOnCompletion = false
gradientChangeAnimation.repeatCount = Float.infinity
gradient.add(gradientChangeAnimation, forKey: "gradientChangeAnimation")

這里運(yùn)行后發(fā)現(xiàn)還是不行,還是只有一種顏色的轉(zhuǎn)換,這是因?yàn)檫@里只調(diào)用了一次animateGradient()。那么如何在合適的時機(jī),也就是動畫結(jié)束的時候再調(diào)用一次animateGradient呢?這里就需要用到CAAnimationDelegate。
在CAAnimationDelegate的animationDidStop方法中重新調(diào)用animateGradient。注意這里的gradient.colors 也要改變,否則就會一直是[colorOne, colorTwo]到其他顏色的變換。

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
? ? ? ??
? ? ? ? // if our gradient animation ended animating, restart the animation by changing the color set
? ? ? ? if flag {
? ? ? ? ? ? gradient.colors = gradientSet[currentGradient]
? ? ? ? ? ? animateGradient()
? ? ? ? }
? ? }

完整代碼:

import UIKit

class ViewController: UIViewController, CAAnimationDelegate {
? ? let colorOne = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1).cgColor
? ? let colorTwo = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1).cgColor
? ? let colorThree = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1).cgColor
? ? let gradient = CAGradientLayer()
? ??
? ? var currentGradient: Int = 0
? ? var gradientSet = [[CGColor]]()
? ??
? ? override func viewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? // Do any additional setup after loading the view.
? ? ? ? NotificationCenter.default.addObserver(self, selector: #selector(handleEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
? ? ? ??
? ? }
? ? override func viewDidLayoutSubviews() {
? ? ? ? super.viewDidLayoutSubviews()
? ? ? ??
? ? ? ? createGradientView()
? ? }
? ??
? ? @objc private func handleEnterForeground() {
? ? ? ? animateGradient()
? ? }
? ??
? ? func animateGradient() {
? ? ? ? // cycle through all the colors, feel free to add more to the set
? ? ? ? if currentGradient < gradientSet.count - 1 {
? ? ? ? ? ? currentGradient += 1
? ? ? ? } else {
? ? ? ? ? ? currentGradient = 0
? ? ? ? }
? ? ? ??
? ? ? ? // animate over 3 seconds
? ? ? ? let gradientChangeAnimation = CABasicAnimation(keyPath: "colors")
? ? ? ? gradientChangeAnimation.duration = 3.0
? ? ? ? gradientChangeAnimation.toValue = gradientSet[currentGradient]
? ? ? ? gradientChangeAnimation.fillMode = CAMediaTimingFillMode.forwards
? ? ? ? gradientChangeAnimation.isRemovedOnCompletion = false
? ? ? ? //gradientChangeAnimation.repeatCount = Float.infinity
? ? ? ? gradientChangeAnimation.delegate = self
? ? ? ? gradient.add(gradientChangeAnimation, forKey: "gradientChangeAnimation")
? ? }
? ??
? ? func createGradientView() {
? ? ? ??
? ? ? ? // overlap the colors and make it 3 sets of colors
? ? ? ? gradientSet.append([colorOne, colorTwo])
? ? ? ? gradientSet.append([colorTwo, colorThree])
? ? ? ? gradientSet.append([colorThree, colorOne])
? ? ? ??
? ? ? ? // set the gradient size to be the entire screen
? ? ? ? gradient.frame = self.view.bounds
? ? ? ? gradient.colors = gradientSet[currentGradient]
? ? ? ? gradient.startPoint = CGPoint(x:0, y:0)
? ? ? ? gradient.endPoint = CGPoint(x:1, y:1)
? ? ? ? gradient.drawsAsynchronously = true
? ? ? ??
? ? ? ? self.view.layer.insertSublayer(gradient, at: 0)
? ? ? ??
? ? ? ? animateGradient()
? ? }
? ? func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
? ? ? ??
? ? ? ? // if our gradient animation ended animating, restart the animation by changing the color set
? ? ? ? if flag {
? ? ? ? ? ? gradient.colors = gradientSet[currentGradient]
? ? ? ? ? ? animateGradient()
? ? ? ? }
? ? }
? ??
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • swift實(shí)現(xiàn)簡單的計(jì)算器

    swift實(shí)現(xiàn)簡單的計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)簡單的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Swift操作Quartz 2D進(jìn)行簡單的繪圖與坐標(biāo)變換的教程

    Swift操作Quartz 2D進(jìn)行簡單的繪圖與坐標(biāo)變換的教程

    這篇文章主要介紹了Swift操作Quartz 2D進(jìn)行簡單的繪圖與坐標(biāo)變換的教程,Quartz 2D是Core Graphics框架中的一個重要組件,經(jīng)常被Mac OS或和iOS開發(fā)者用來繪圖,需要的朋友可以參考下
    2016-04-04
  • 深入理解Swift語言中的閉包機(jī)制

    深入理解Swift語言中的閉包機(jī)制

    這篇文章主要介紹了Swift語言中的閉包機(jī)制,是Swift入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11
  • Swift 4最全的新特性詳細(xì)解析(推薦)

    Swift 4最全的新特性詳細(xì)解析(推薦)

    Swift 4 在 Swift 3 的基礎(chǔ)上,提供了更強(qiáng)大的穩(wěn)健性和穩(wěn)定性。所以下面這篇文章就來給大家總結(jié)介紹關(guān)于Swift4新特性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-11-11
  • 利用Swift如何計(jì)算文本的size示例詳解

    利用Swift如何計(jì)算文本的size示例詳解

    這篇文章主要給大家介紹了關(guān)于利用Swift如何計(jì)算文本的size的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們的工作或者學(xué)習(xí)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • 舉例講解Swift編程中switch...case語句的用法

    舉例講解Swift編程中switch...case語句的用法

    這篇文章主要介紹了Swift編程中switch...case語句的用法,其中fallthrough關(guān)鍵字在switch語句中的使用是重點(diǎn),需要的朋友可以參考下
    2016-04-04
  • 在?Swift?中編寫Git?Hooks腳本的方法

    在?Swift?中編寫Git?Hooks腳本的方法

    在本例中,我使用了?commit-msg?鉤子,它能夠在當(dāng)前提交信息生效前修改此信息,鉤子由一個參數(shù)調(diào)用,該參數(shù)是指向包含用戶輸入的提交消息的文件的路徑,這意味著,為了改變提交消息,我們只需要從文件中讀取、修改其內(nèi)容,然后寫回調(diào)用掛鉤的文件
    2022-06-06
  • Swift算法之棧和隊(duì)列的實(shí)現(xiàn)方法示例

    Swift算法之棧和隊(duì)列的實(shí)現(xiàn)方法示例

    Swift語言中沒有內(nèi)設(shè)的棧和隊(duì)列,很多擴(kuò)展庫中使用Generic Type來實(shí)現(xiàn)?;蚴顷?duì)列。下面這篇文章就來給大家詳細(xì)介紹了Swift算法之棧和隊(duì)列的實(shí)現(xiàn)方法,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。
    2017-03-03
  • 分析Swift性能高效的原因

    分析Swift性能高效的原因

    絕大多數(shù)公司選擇Swift語言開發(fā)iOS應(yīng)用,主要原因是因?yàn)镾wift相比Objc有更快的運(yùn)行效率,更加安全的類型檢測,更多現(xiàn)代語言的特性提升開發(fā)效率;這一系列的優(yōu)點(diǎn)使Swift語言的熱度越來越高。
    2020-10-10
  • Swift初始化器與可選鏈的使用方法介紹

    Swift初始化器與可選鏈的使用方法介紹

    初始化器初始化是準(zhǔn)備類、結(jié)構(gòu)或枚舉的實(shí)例以供使用的過程。此過程涉及為該實(shí)例上的每個存儲屬性設(shè)置初始值,并執(zhí)行在新實(shí)例準(zhǔn)備就緒可供使用之前所需的任何其他設(shè)置或初始化,可選鏈?zhǔn)且环N可以請求和調(diào)用屬性、方法和子腳本的過程,用于請求或調(diào)用的目標(biāo)可能為nil
    2022-08-08

最新評論

金乡县| 韶山市| 长丰县| 恩平市| 天长市| 东阿县| 台南市| 东安县| 新闻| 修水县| 封开县| 门头沟区| 南江县| 思南县| 长治县| 海门市| 新昌县| 拜泉县| 锦州市| 家居| 闻喜县| 化德县| 同德县| 苍山县| 黎川县| 南京市| 林州市| 宝清县| 栾城县| 蒙山县| 南昌县| 泊头市| 景谷| 江阴市| 嘉峪关市| 土默特左旗| 含山县| 武宣县| 江孜县| 缙云县| 金平|