UIBezierPathStudyDemo

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import UIKit
import XCPlayground
 
//创建view
let myView = UIView(frame:CGRectMake(0, 0, 300, 200))
//实时显示TimeLine
XCPlaygroundPage.currentPage.liveView = myView
myView.backgroundColor = UIColor.whiteColor()
 
 
/************ 打开相应的注释, 就会自动显示在Timeline里面了*******/
 
/***********************基础使用****************************/
// 创建layer
let myLayer = CAShapeLayer()
myLayer.frame = CGRectMake(0, 0
    , 100, 50)
//myLayer.backgroundColor = UIColor.yellowColor().CGColor
 
//创建贝塞尔曲线
//画正方形
let path = UIBezierPath(rect: CGRectMake(50, 10, 50, 50))
//圆角长方形
let path2 = UIBezierPath(roundedRect: CGRectMake(50, 10, 80, 50), cornerRadius: 25)
 
 
//画圆
let radius:CGFloat = 30.0;
let startAngle:CGFloat = 0.0;
let endAngle:CGFloat = CGFloat(M_PI * 2)
 
let path3 = UIBezierPath(arcCenter: myView.center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
 
//赋值path绘图案
myLayer.path = path3.CGPath;
//layer填充颜色
myLayer.fillColor = UIColor.redColor().CGColor
 
//myLayer.fillColor = UIColor.clearColor().CGColor
//layer描边颜色
myLayer.strokeColor = UIColor.whiteColor().CGColor
//添加layerd到view
myView.layer.addSublayer(myLayer)
 
/***********************基础使用****************************/
 
/*
/***************通过控制点画贝塞尔曲线********************/
//开始点
let startPoint   = CGPointMake(50, 100)
//结束点
let endPoint     = CGPointMake(250, 100)
//中间控制点
//let controlPoint = CGPointMake(150, 40)
let controlPoint = CGPointMake(116, 40)
let controlPoint2 = CGPointMake(182, 180)
//红色标示正方形begin
let layer1             = CALayer()
layer1.frame           = CGRectMake(startPoint.x, startPoint.y, 5, 5)
layer1.backgroundColor = UIColor.redColor().CGColor
let layer2             = CALayer()
layer2.frame           = CGRectMake(endPoint.x, endPoint.y, 5, 5)
layer2.backgroundColor = UIColor.redColor().CGColor
let layer3             = CALayer()
layer3.frame           = CGRectMake(controlPoint.x, controlPoint.y, 5, 5)
layer3.backgroundColor = UIColor.redColor().CGColor
let layer4             = CALayer()
layer4.frame           = CGRectMake(controlPoint2.x - 2.5, controlPoint2.y - 5, 5, 5)
layer4.backgroundColor = UIColor.redColor().CGColor
//红色标示正方形end
//初始化
let path  = UIBezierPath()
let layer = CAShapeLayer()
//设置开始点
path.moveToPoint(startPoint)
//画线路径 参数:(结束点, 控制点)
//path.addQuadCurveToPoint(endPoint, controlPoint: controlPoint)
//多个控制点
path.addCurveToPoint(endPoint, controlPoint1: controlPoint, controlPoint2: controlPoint2)
layer.path        = path.CGPath
layer.fillColor   = UIColor.clearColor().CGColor
layer.strokeColor = UIColor.blackColor().CGColor
myView.layer.addSublayer(layer)
myView.layer.addSublayer(layer1)
myView.layer.addSublayer(layer2)
myView.layer.addSublayer(layer3)
myView.layer.addSublayer(layer4)
/***************通过控制点画贝塞尔曲线********************/
*/
 
/*
/***********************添加动画****************************/
private func animation1() {
    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = 0
    animation.toValue = 1
    animation.duration = 2
    layer.addAnimation(animation, forKey: "")
}
private func animation2() {
    layer.strokeStart = 0.5
    layer.strokeEnd = 0.5
     
    let animation = CABasicAnimation(keyPath: "strokeStart")
    animation.fromValue = 0.5
    animation.toValue = 0
    animation.duration = 2
     
    let animation2 = CABasicAnimation(keyPath: "strokeEnd")
    animation2.fromValue = 0.5
    animation2.toValue = 1
    animation2.duration = 2
     
    layer.addAnimation(animation, forKey: "")
    layer.addAnimation(animation2, forKey: "")
}
private func animation3() {
    let animation = CABasicAnimation(keyPath: "lineWidth")
    animation.fromValue = 1
    animation.toValue = 10
    animation.duration = 2
    layer.addAnimation(animation, forKey: "")
}
//animation1()
animation2()
animation3()
/***********************添加动画****************************/
*/
 
/*
/******************画不标准图形**********************/
let finalSize = CGSizeMake(CGRectGetWidth(myView.frame), 200)
let layerHeight = finalSize.height * 0.2
let layer = CAShapeLayer()
let bezier = UIBezierPath()
//左上角点
bezier.moveToPoint(CGPointMake(0, finalSize.height - layerHeight))
//画线到左下角
bezier.addLineToPoint(CGPointMake(0, finalSize.height - 1))
//画线到右下角
bezier.addLineToPoint(CGPointMake(finalSize.width, finalSize.height-1))
//画线到右上角
bezier.addLineToPoint(CGPointMake(finalSize.width, finalSize.height - layerHeight))
//从右上角画贝塞尔曲线到左上角
let controlP = CGPointMake(finalSize.width * 0.5, finalSize.height - layerHeight * 2)
bezier.addQuadCurveToPoint(CGPointMake(0, finalSize.height - layerHeight), controlPoint: controlP)
layer.path = bezier.CGPath
layer.fillColor = UIColor.redColor().CGColor
myView.layer.addSublayer(layer)
/******************画不标准图形**********************/
*/
 
 
/*
/******************画微信眼睛**********************/
//first
let eyeFirstLightLayer = CAShapeLayer()
let centerPoint        = CGPointMake(CGRectGetWidth(myView.frame) * 0.5, CGRectGetHeight(myView.frame) * 0.5)
let startAngle         = CGFloat(230.0 / 180.0 * M_PI)
let endAngle           = CGFloat(265.0 / 180.0 * M_PI)
let path = UIBezierPath(arcCenter: centerPoint, radius: CGRectGetWidth(myView.frame) * 0.1, startAngle:startAngle, endAngle: endAngle, clockwise: true)
eyeFirstLightLayer.borderColor = UIColor.blackColor().CGColor
eyeFirstLightLayer.lineWidth   = 5.0
eyeFirstLightLayer.path        = path.CGPath
eyeFirstLightLayer.fillColor   = UIColor.clearColor().CGColor
eyeFirstLightLayer.strokeColor = UIColor.darkGrayColor().CGColor
myView.layer.addSublayer(eyeFirstLightLayer)
//second
let eyeSecondLightLayer = CAShapeLayer()
let centerPoint2        = CGPointMake(CGRectGetWidth(myView.frame) * 0.5, CGRectGetHeight(myView.frame) * 0.5)
let startAngle2         = CGFloat(211.0 / 180.0 * M_PI)
let endAngle2           = CGFloat(220.0 / 180.0 * M_PI)
let path2 = UIBezierPath(arcCenter: centerPoint2, radius: CGRectGetWidth(myView.frame) * 0.1, startAngle:startAngle2, endAngle: endAngle2, clockwise: true)
eyeSecondLightLayer.borderColor = UIColor.blackColor().CGColor
eyeSecondLightLayer.lineWidth   = 5.0
eyeSecondLightLayer.path        = path2.CGPath
eyeSecondLightLayer.fillColor   = UIColor.clearColor().CGColor
eyeSecondLightLayer.strokeColor = UIColor.darkGrayColor().CGColor
myView.layer.addSublayer(eyeSecondLightLayer)
//ball
let eyeBallLayer    = CAShapeLayer()
let centerPointBall = CGPointMake(CGRectGetWidth(myView.frame) * 0.5, CGRectGetHeight(myView.frame) * 0.5)
let startAngleBall = CGFloat(0 / 180.0 * M_PI)
let endAngleBall   = CGFloat(360.0 / 180.0 * M_PI)
let pathBall = UIBezierPath(arcCenter: centerPointBall, radius: CGRectGetWidth(myView.frame) * 0.15, startAngle:startAngleBall, endAngle: endAngleBall, clockwise: true)
eyeBallLayer.borderColor = UIColor.blackColor().CGColor
eyeBallLayer.lineWidth   = 1.0
eyeBallLayer.path        = pathBall.CGPath
eyeBallLayer.fillColor   = UIColor.clearColor().CGColor
eyeBallLayer.strokeColor = UIColor.darkGrayColor().CGColor
eyeBallLayer.anchorPoint = CGPointMake(0.5, 0.5)
myView.layer.addSublayer(eyeBallLayer)
//topEye
let topEyeLayer       = CAShapeLayer()
let centerPointTopEye = CGPointMake(CGRectGetWidth(myView.frame) * 0.5, CGRectGetHeight(myView.frame) * 0.5)
let startAngleTopEye = CGFloat(0 / 180.0 * M_PI)
let endAngleTopEye   = CGFloat(360.0 / 180.0 * M_PI)
let pathTopEye = UIBezierPath()
pathTopEye.moveToPoint(CGPointMake(0, CGRectGetHeight(myView.frame) * 0.5))
pathTopEye.addQuadCurveToPoint(CGPointMake(CGRectGetWidth(myView.frame), CGRectGetHeight(myView.frame) * 0.5), controlPoint: CGPointMake(CGRectGetWidth(myView.frame) * 0.5, centerPointTopEye.y - centerPointTopEye.y - 20))
topEyeLayer.borderColor = UIColor.blackColor().CGColor
topEyeLayer.lineWidth   = 1.0
topEyeLayer.path        = pathTopEye.CGPath
topEyeLayer.fillColor   = UIColor.clearColor().CGColor
topEyeLayer.strokeColor = UIColor.darkGrayColor().CGColor
myView.layer.addSublayer(topEyeLayer)
//BottomEye
let bottomEyeLayer       = CAShapeLayer()
let centerPointBottomEye = CGPointMake(CGRectGetWidth(myView.frame) * 0.5, CGRectGetHeight(myView.frame) * 0.5)
let startAngleBottomEye = CGFloat(0 / 180.0 * M_PI)
let endAnglebottomEye   = CGFloat(360.0 / 180.0 * M_PI)
let pathBottomEye = UIBezierPath()
pathBottomEye.moveToPoint(CGPointMake(0, CGRectGetHeight(myView.frame) * 0.5))
pathBottomEye.addQuadCurveToPoint(CGPointMake(CGRectGetWidth(myView.frame), CGRectGetHeight(myView.frame) * 0.5), controlPoint: CGPointMake(CGRectGetWidth(myView.frame) * 0.5, centerPointBottomEye.y * 2 + 20))
bottomEyeLayer.borderColor = UIColor.blackColor().CGColor
bottomEyeLayer.lineWidth   = 1.0
bottomEyeLayer.path        = pathBottomEye.CGPath
bottomEyeLayer.fillColor   = UIColor.clearColor().CGColor
bottomEyeLayer.strokeColor = UIColor.darkGrayColor().CGColor
myView.layer.addSublayer(bottomEyeLayer)
// 动画
private func setupAnimation(){
    eyeFirstLightLayer.lineWidth  = 0.0
    eyeSecondLightLayer.lineWidth = 0.0
    eyeBallLayer.opacity          = 0.0
    bottomEyeLayer.strokeStart    = 0.5
    bottomEyeLayer.strokeEnd      = 0.5
    topEyeLayer.strokeStart       = 0.5
    topEyeLayer.strokeEnd         = 0.5
}
private func eyeAnimation1() {
    let animation = CABasicAnimation(keyPath: "lineWidth")
    animation.fromValue = 0.0
    animation.toValue   = 5.0
    animation.duration  = 2
     
    let animation2 = CABasicAnimation(keyPath: "lineWidth")
    animation2.fromValue = 0.0
    animation2.toValue   = 5.0
    animation2.duration  = 2
     
    let animation3 = CABasicAnimation(keyPath: "opacity")
    animation3.fromValue = 0.0
    animation3.toValue   = 5.0
    animation3.duration  = 5
    eyeFirstLightLayer.addAnimation(animation, forKey: "")
    eyeSecondLightLayer.addAnimation(animation2, forKey: "")
    eyeBallLayer.addAnimation(animation3, forKey: "")
    eyeFirstLightLayer.lineWidth  = 5.0
    eyeSecondLightLayer.lineWidth = 5.0
    eyeBallLayer.opacity          = 1.0
}
private func topEyeLayerAnimation() {
    let animation = CABasicAnimation(keyPath: "strokeStart")
    animation.fromValue = 0.5
    animation.toValue   = 0
    animation.duration  = 2
     
    let animation2 = CABasicAnimation(keyPath: "strokeEnd")
    animation2.fromValue = 0.5
    animation2.toValue   = 1
    animation2.duration  = 2
     
    topEyeLayer.addAnimation(animation, forKey: "")
    topEyeLayer.addAnimation(animation2, forKey: "")
    topEyeLayer.strokeStart    = 0
    topEyeLayer.strokeEnd      = 1
}
private func eyeBottomAnimation() {
    let animation = CABasicAnimation(keyPath: "strokeStart")
    animation.fromValue = 0.5
    animation.toValue   = 0
    animation.duration  = 2
     
    let animation2 = CABasicAnimation(keyPath: "strokeEnd")
    animation2.fromValue = 0.5
    animation2.toValue   = 1
    animation2.duration  = 2
     
    bottomEyeLayer.addAnimation(animation, forKey: "")
    bottomEyeLayer.addAnimation(animation2, forKey: "")
    bottomEyeLayer.strokeStart    = 0
    bottomEyeLayer.strokeEnd      = 1
}
//初始化
setupAnimation()
//中间圆动画
eyeAnimation1()
//眼睛上部分动画
topEyeLayerAnimation()
//眼睛下部分动画
eyeBottomAnimation()
/******************画微信眼睛**********************/
*/

 

posted @   brave-sailor  阅读(156)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2015-10-14 最简单也最难——如何获取到Android控件的高度
2015-10-14 Android新增API之AudioEffect中文API与应用实例
2013-10-14 Android之动态改变控件大小
2013-10-14 android适配各种分辨率的问题
点击右上角即可分享
微信分享提示