服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Swift - swift实现颜色渐变以及转换动画

swift实现颜色渐变以及转换动画

2022-07-21 10:44LinShunIos Swift

这篇文章主要为大家详细介绍了swift实现颜色渐变以及转换动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文是通过结合使用CAGradientLayer、CABasicAnimation以及CAAnimationDelegate来达到颜色渐变以及转换的动画,下面是今天要达成的效果图:

swift实现颜色渐变以及转换动画

首先创建一个CAGradientLayer和几个自己喜欢的颜色,让VC持有。

?
1
2
3
4
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等同于视图的大小,然后颜色先设置为colorOne和colorTwo,起始点和结束点分别为CGPoint(x:0, y:0)和CGPoint(x:1, y:1),并设置让其在后台线程异步绘制,最后添加到view的layer的sublayer中。

?
1
2
3
4
5
6
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)

现在运行后会得到下面的结果:

swift实现颜色渐变以及转换动画

颜色渐变是做到了,那么如何做到颜色渐变的转换呢?这里还是需要用到CABasicAnimation.
在gradient创建完之后,添加并调用一个方法animateGradient,在里面添加一个keyPath为colors的CABasicAnimation,设置动画时长为3s,设置结束值等一系列属性。

?
1
2
3
4
5
6
7
8
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")
      }

这里就完成了转换动画。但是这里有个问题就是这里只转换了一次,无法转换多次颜色。那么这里就需要设置好toValue,让每次的toValue都不一样。
创建一个currentGradient和gradientSet让VC持有。

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

在animateGradient中每次调用的时候,都对currentGradient的值进行判断和处理。

?
1
2
3
4
5
if currentGradient < gradientSet.count - 1 {
            currentGradient += 1
        } else {
            currentGradient = 0
        }

并修改gradientChangeAnimation的toValue:

?
1
2
3
4
5
6
7
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")

这里运行后发现还是不行,还是只有一种颜色的转换,这是因为这里只调用了一次animateGradient()。那么如何在合适的时机,也就是动画结束的时候再调用一次animateGradient呢?这里就需要用到CAAnimationDelegate。
在CAAnimationDelegate的animationDidStop方法中重新调用animateGradient。注意这里的gradient.colors 也要改变,否则就会一直是[colorOne, colorTwo]到其他颜色的变换。

?
1
2
3
4
5
6
7
8
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()
        }
    }

完整代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()
        }
    }
    
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/LinShunIos/article/details/120669939

延伸 · 阅读

精彩推荐
  • SwiftSwift利用纯代码实现时钟效果实例代码

    Swift利用纯代码实现时钟效果实例代码

    这篇文章主要给大家介绍了关于Swift利用纯代码实现时钟效果的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用swift具有一定的参考学习...

    OneSwift7032021-01-11
  • SwiftSwift语言中的一些访问控制设置详解

    Swift语言中的一些访问控制设置详解

    这篇文章主要介绍了Swift语言中的一些访问控制设置详解,是Swift入门学习中的基础知识,需要的朋友可以参考下...

    Swift教程网2052020-12-24
  • Swift利用Swift如何判断iPhone X机型详解

    利用Swift如何判断iPhone X机型详解

    近日,iPhone X的发布在人们群众引起了很大的轰动,下面这篇文章主要给大家介绍了关于利用Swift如何判断iPhone X机型的相关资料,文中通过示例代码介绍的...

    TUALATRIX13132021-01-06
  • SwiftSwift与C语言指针结合使用实例

    Swift与C语言指针结合使用实例

    这篇文章主要介绍了Swift与C语言指针结合使用实例,本文讲解了用以输入/输出的参数指针、作为数组使用的参数指针、用作字符串参数的指针、指针参数转...

    Swift教程网1972020-12-20
  • SwiftSwift心得笔记之集合类型

    Swift心得笔记之集合类型

    本文为大家讲解的是swift语言中的集合类型数据,这是swift开发必须掌握的知识点,感兴趣的同学参考下。...

    Swift教程网2552020-12-19
  • SwiftSwift数组详细用法解析

    Swift数组详细用法解析

    这篇文章主要为大家详细介绍了Swift数组详细用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    追到梦的魔术师8902020-12-30
  • Swift通过Notification.Name看Swift是如何优雅的解决String硬编码

    通过Notification.Name看Swift是如何优雅的解决String硬编码

    这篇文章主要给大家介绍了通过Notification.Name看Swift是如何优雅的解决String硬编码的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具...

    韩元旭7782021-01-12
  • Swift简单了解Swift语言中的break和continue语句的用法

    简单了解Swift语言中的break和continue语句的用法

    这篇文章主要简单介绍了Swift语言中的break和continue语句的用法,与其他语言的一样用于循环语句流程控制,需要的朋友可以参考下...

    Swift教程网5382020-12-22