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

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

服务器之家 - 编程语言 - Swift - Swift绘制渐变色的方法

Swift绘制渐变色的方法

2021-12-24 14:11风浅月明 Swift

这篇文章主要为大家详细介绍了Swift绘制渐变色的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了swift绘制渐变色的具体代码,供大家参考,具体内容如下

示意图:

Swift绘制渐变色的方法

?
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
import foundation
import uikit
 
class gradientvc: uiviewcontroller {
    
    @iboutlet weak var butone: gradientcustombutton!
    @iboutlet weak var viewtwo: uiview!
    
    override func viewdidload() {
        super.viewdidload()
        
        /// 方式一 xib添加渐变色
        
        /// 方式一 代码添加渐变色
        butone.isgradient = true
        butone.startcolor = uicolor(hexstring: "#fd0134")!
        butone.endcolor = uicolor(hexstring: "#007aff")!
        butone.startpoint = cgpoint(x: 0,y: 0)
        butone.endpoint = cgpoint(x: 1,y: 1)
        
        /// 方式二
        //viewtwo.addgradient(start_color: "#8238ff", end_color: "#007aff")
        //viewtwo.layer.maskstobounds = true
        viewtwo.addgradient(colors: [uicolor(hexstring: "#fd0134")!, uicolor(hexstring: "#007aff")!],
                            point: (cgpoint(x: 1.0, y: 0.0), cgpoint(x: 0.0, y: 1.0)),
                            frame: cgrect(x: 0, y: 0, width: uiscreen.main.bounds.width-40, height: 100),
                            radius: 0)
 
    }
}

方式一:

使用xib或代码的方式添加渐变色.

Swift绘制渐变色的方法

Swift绘制渐变色的方法

Swift绘制渐变色的方法

这种方式有个缺点, 若是要对更多的视图(比如uilabel)添加渐变色, 需要继续创建一个子类继承于它进行功能的拓展.

?
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
import foundation
import uikit
 
class gradientcustomview: uiview {
    
    @ibinspectable var isgradient: bool = false
    @ibinspectable var startcolor: uicolor = .white
    @ibinspectable var endcolor: uicolor = .white
    @ibinspectable var locations: [nsnumber] = [0 , 1]
    @ibinspectable var startpoint: cgpoint = .zero
    @ibinspectable var endpoint: cgpoint = .zero
    
    private var gradientbglayer: cagradientlayer?
    
    override func layoutsubviews() {
        super.layoutsubviews()
        gradientbglayer?.removefromsuperlayer()
        if isgradient {
            gradientbglayer = cagradientlayer()
            gradientbglayer!.colors = [startcolor.cgcolor, endcolor.cgcolor]
            gradientbglayer!.locations = locations
            gradientbglayer!.frame = bounds
            gradientbglayer!.startpoint = startpoint
            gradientbglayer!.endpoint = endpoint
            self.layer.insertsublayer(gradientbglayer!, at: 0)
        }
    }
 
}
 
class gradientcustombutton: uibutton {
    
    @ibinspectable var isgradient: bool = false
    @ibinspectable var startcolor: uicolor = .white
    @ibinspectable var endcolor: uicolor = .white
    @ibinspectable var locations: [nsnumber] = [0 , 1]
    @ibinspectable var startpoint: cgpoint = .zero
    @ibinspectable var endpoint: cgpoint = .zero
    
    private var gradientbglayer: cagradientlayer?
    
    override func layoutsubviews() {
        super.layoutsubviews()
        gradientbglayer?.removefromsuperlayer()
        if isgradient {
            gradientbglayer = cagradientlayer()
            gradientbglayer!.colors = [startcolor.cgcolor, endcolor.cgcolor]
            gradientbglayer!.locations = locations
            gradientbglayer!.frame = bounds
            gradientbglayer!.startpoint = startpoint
            gradientbglayer!.endpoint = endpoint
            self.layer.insertsublayer(gradientbglayer!, at: 0)
        }
    }
    
}

方式二:

直接拓展uiview,让每个继承于uiview的视图都可以调用拓展的方法.

这种方式的缺点就是无法在xib中使用

?
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import foundation
import uikit
 
extension uiview {
    
    @discardableresult
    func addgradient(colors: [uicolor],
                     point: (cgpoint, cgpoint) = (cgpoint(x: 0.5, y: 0), cgpoint(x: 0.5, y: 1)),
                     locations: [nsnumber] = [0, 1],
                     frame: cgrect = cgrect.zero,
                     radius: cgfloat = 0,
                     at: uint32 = 0) -> cagradientlayer {
        let bglayer1 = cagradientlayer()
        bglayer1.colors = colors.map { $0.cgcolor }
        bglayer1.locations = locations
        if frame == .zero {
            bglayer1.frame = self.bounds
        } else {
            bglayer1.frame = frame
        }
        bglayer1.startpoint = point.0
        bglayer1.endpoint = point.1
        bglayer1.cornerradius = radius
        self.layer.insertsublayer(bglayer1, at: at)
        return bglayer1
    }
    
    func addgradient(start: cgpoint = cgpoint(x: 0.5, y: 0),
                     end: cgpoint = cgpoint(x: 0.5, y: 1),
                     colors: [uicolor],
                     locations: [nsnumber] = [0, 1],
                     frame: cgrect = cgrect.zero,
                     radius: cgfloat = 0,
                     at: uint32 = 0) {
        let bglayer1 = cagradientlayer()
        bglayer1.colors = colors.map { $0.cgcolor }
        bglayer1.locations = locations
        bglayer1.frame = frame
        bglayer1.startpoint = start
        bglayer1.endpoint = end
        bglayer1.cornerradius = radius
        self.layer.insertsublayer(bglayer1, at: at)
    }
    
    func addgradient(start_color:string,end_color : string,frame : cgrect?=nil,cornerradius : cgfloat?=0, at: uint32 = 0){
        var bounds : cgrect = self.bounds
        if let frame = frame {
            bounds = frame
        }
        let bglayer1 = cagradientlayer()
        bglayer1.colors = [uicolor(hexstring: start_color)!.cgcolor, uicolor(hexstring: end_color)!.cgcolor]
        bglayer1.locations = [0, 1]
        bglayer1.frame = bounds
        bglayer1.startpoint = cgpoint(x: 0, y: 0.61)
        bglayer1.endpoint = cgpoint(x: 0.61, y: 0.61)
        bglayer1.cornerradius = cornerradius ?? 0
        self.layer.insertsublayer(bglayer1, at: at)
    }
    
    func addgradient(start_color:string,
                     end_color : string,
                     frame : cgrect?=nil,
                     borader: cgfloat = 0,
                     boradercolor: uicolor = .clear,
                     at: uint32 = 0,
                     corners: uirectcorner?,
                     radius: cgfloat = 0) {
        var bounds : cgrect = self.bounds
        if let frame = frame {
            bounds = frame
        }
        let bglayer1 = cagradientlayer()
        bglayer1.colors = [uicolor(hexstring: start_color)!.cgcolor, uicolor(hexstring: end_color)!.cgcolor]
        bglayer1.locations = [0, 1]
        bglayer1.frame = bounds
        bglayer1.startpoint = cgpoint(x: 0, y: 0.61)
        bglayer1.endpoint = cgpoint(x: 0.61, y: 0.61)
        bglayer1.bordercolor = boradercolor.cgcolor
        bglayer1.borderwidth = borader
        if corners != nil {
            let cornerpath = uibezierpath(roundedrect: bounds, byroundingcorners: corners!, cornerradii: cgsize(width: radius, height: radius))
            let radiuslayer = cashapelayer()
            radiuslayer.frame = bounds
            radiuslayer.path = cornerpath.cgpath
            bglayer1.mask = radiuslayer
        }
        self.layer.insertsublayer(bglayer1, at: at)
    }
    
    func addgradient(startpoint: cgpoint = cgpoint(x: 0, y: 0.5),
                     start_color:string,
                     endpoint: cgpoint = cgpoint(x: 1, y: 0.5),
                     end_color : string,
                     frame : cgrect? = nil,
                     cornerradius : cgfloat?=0){
        var bounds : cgrect = self.bounds
        if let frame = frame {
            bounds = frame
        }
        let bglayer1 = cagradientlayer()
        bglayer1.frame = bounds
        bglayer1.startpoint = startpoint
        bglayer1.endpoint = endpoint
        bglayer1.colors = [uicolor(hexstring: start_color)!.cgcolor, uicolor(hexstring: end_color)!.cgcolor]
        bglayer1.locations = [0, 1]
        bglayer1.cornerradius = cornerradius ?? 0
        self.layer.addsublayer(bglayer1)
    }
    
    func addverticalgradient(start_color:string,end_color : string,frame : cgrect?=nil,cornerradius : cgfloat?=0){
        var bounds : cgrect = self.bounds
        if let frame = frame {
            bounds = frame
        }
        let bglayer1 = cagradientlayer()
        bglayer1.colors = [uicolor(hexstring: start_color)!.cgcolor, uicolor(hexstring: end_color)!.cgcolor]
        bglayer1.locations = [0, 1]
        bglayer1.frame = bounds
        bglayer1.startpoint = cgpoint(x: 0.5, y: 0)
        bglayer1.endpoint = cgpoint(x: 1, y: 1)
        bglayer1.cornerradius = cornerradius ?? 0
        self.layer.insertsublayer(bglayer1, at: 0)
    }
    
    //将当前视图转为uiimage
    func asimage() -> uiimage {
        let renderer = uigraphicsimagerenderer(bounds: bounds)
        return renderer.image { renderercontext in
            layer.render(in: renderercontext.cgcontext)
        }
    }
}

demo:https://github.com/Gamin-fzym/GAGradientRampDemo.git

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

原文链接:https://blog.csdn.net/wsyx768/article/details/119422014

延伸 · 阅读

精彩推荐
  • Swiftswift相册相机的权限处理示例详解

    swift相册相机的权限处理示例详解

    在iOS7以后要打开手机摄像头或者相册的话都需要权限,在iOS9中更是更新了相册相关api的调用,那么下面这篇文章主要给大家介绍了关于swift相册相机权限处...

    hello老文12682021-01-08
  • Swift分析Swift性能高效的原因

    分析Swift性能高效的原因

    绝大多数公司选择Swift语言开发iOS应用,主要原因是因为Swift相比Objc有更快的运行效率,更加安全的类型检测,更多现代语言的特性提升开发效率;这一系...

    louis_wang9092021-01-16
  • Swift详解Swift 之clipped是什么如何用

    详解Swift 之clipped是什么如何用

    这篇文章主要介绍了详解Swift 之clipped是什么如何用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    iCloudEnd8532021-05-28
  • SwiftSwift算法之栈和队列的实现方法示例

    Swift算法之栈和队列的实现方法示例

    Swift语言中没有内设的栈和队列,很多扩展库中使用Generic Type来实现栈或是队列。下面这篇文章就来给大家详细介绍了Swift算法之栈和队列的实现方法,需要...

    李峰峰10002021-01-05
  • SwiftSwift 基本数据类型详解总结

    Swift 基本数据类型详解总结

    在我们使用任何程序语言编程时,需要使用各种数据类型来存储不同的信息。变量的数据类型决定了如何将代表这些值的位存储到计算机的内存中。在声明...

    Lucky_William4672021-12-26
  • SwiftSwift网络请求库Alamofire使用详解

    Swift网络请求库Alamofire使用详解

    这篇文章主要为大家详细介绍了Swift网络请求库Alamofire的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    lv灬陈强56682021-01-06
  • SwiftSwift中排序算法的简单取舍详解

    Swift中排序算法的简单取舍详解

    对于排序算法, 通常简单的, 为大家所熟知的有, 选择排序, 冒泡排序, 快速排序, 当然还有哈希, 桶排序之类的, 本文仅比较最为常见的选择, 冒泡和快排,文...

    Castie111012021-01-10
  • Swift浅谈在Swift中关于函数指针的实现

    浅谈在Swift中关于函数指针的实现

    这篇文章主要介绍了浅谈在Swift中关于函数指针的实现,是作者根据C语言的指针特性在Swifft中做出的一个实验,需要的朋友可以参考下...

    Swift教程网4372020-12-21