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

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

服务器之家 - 编程语言 - IOS - IOS使用progssview仿滴滴打车圆形计时

IOS使用progssview仿滴滴打车圆形计时

2020-12-22 16:17iOS开发网 IOS

本文给大家分享的是IOS中实现仿滴滴打车的原型计时效果,非常的实用,有需要的小伙伴可以参考下。

实现类似在微信中使用的滴滴打车的progressview,实现效果如图

IOS使用progssview仿滴滴打车圆形计时

?
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//
// ccprogressview.h
// hurricaneconsumer
//
// created by wangcong on 15-3-25.
// copyright (c) 2015年 wangcong. all rights reserved.
//
 
#import <uikit/uikit.h>
#import <quartzcore/quartzcore.h>
 
/**
 * 动画开始
 */
typedef void(^block_progress_start)();
 
/**
 * 动画正在进行
 * @param nstimeinterval
 */
typedef void(^block_progress_animing)(nstimeinterval);
 
/**
 * 动画结束
 */
typedef void(^block_progress_stop)();
 
@interface ccprogressview : uiview
{
  nstimeinterval _animationtime;
}
 
@property (nonatomic, strong) uilabel *centerlabel;    // 中心label
 
@property (nonatomic, copy) block_progress_start start;   // 动画开始回调
@property (nonatomic, copy) block_progress_animing animing; // 动画进行
@property (nonatomic, copy) block_progress_stop stop;    // 动画结束回调
 
- (void) setanimationtime:(nstimeinterval)animationtime;
 
- (void)startanimation;
 
- (void)stopanimation;
 
@end
 
 
//
// ccprogressview.m
// hurricaneconsumer
//
// created by wangcong on 15-3-25.
// copyright (c) 2015年 wangcong. all rights reserved.
//
 
#import "ccprogressview.h"
 
#define kprogressthumbwh 30
 
// 计时器间隔时长
#define kanimtimeinterval 0.1
 
/**
 * 圆圈layer上旋转的layer
 */
@interface ccprogressthumb : calayer
{
  nstimeinterval _animationtime;
}
 
@property (assign, nonatomic) double startangle;
@property (nonatomic, strong) uilabel *timelabel;      // 显示时间label
 
@end
 
@implementation ccprogressthumb
 
- (instancetype)init
{
  if ((self = [super init])) {
    [self setuplayer];
  }
  return self;
}
 
- (void)layoutsublayers
{
  _timelabel.frame = self.bounds;
   
  [_timelabel sizetofit];
  _timelabel.center = cgpointmake(cgrectgetmidx(self.bounds) - _timelabel.frame.origin.x,
                  cgrectgetmidy(self.bounds) - _timelabel.frame.origin.y);
}
 
- (void)setuplayer
{
  // 绘制圆
  uigraphicsbeginimagecontext(cgsizemake(kprogressthumbwh, kprogressthumbwh));
  cgcontextref ctx = uigraphicsgetcurrentcontext();
  cgcontextsetlinewidth(ctx, 1);
  cgcontextsetfillcolorwithcolor(ctx, [uicolor lightgraycolor].cgcolor);
  cgcontextsetstrokecolorwithcolor(ctx, [uicolor lightgraycolor].cgcolor);
  cgcontextaddellipseinrect(ctx, cgrectmake(1, 1, kprogressthumbwh - 2, kprogressthumbwh - 2));
  cgcontextdrawpath(ctx, kcgpathfillstroke);
  uiimage *circle = uigraphicsgetimagefromcurrentimagecontext();
  uigraphicsendimagecontext();
   
  uiimageview *circleview = [[uiimageview alloc] initwithimage:circle];
  circleview.frame = cgrectmake(0, 0, kprogressthumbwh, kprogressthumbwh);
  circleview.image = circle;
  [self addsublayer:circleview.layer];
   
  _timelabel = [[uilabel alloc] initwithframe:self.bounds];
  _timelabel.textcolor = [uicolor redcolor];
  _timelabel.font = [uifont systemfontofsize:10];
  _timelabel.textalignment = nstextalignmentcenter;
  _timelabel.text = @"00:00";
  [self addsublayer:_timelabel.layer];
   
  _startangle = - m_pi / 2;
}
 
- (void)setanimationtime:(nstimeinterval)animationtime
{
  _animationtime = animationtime;
}
 
- (double)calculatepercent:(nstimeinterval)fromtime totime:(nstimeinterval)totime
{
  double progress = 0.0f;
  if ((totime > 0) && (fromtime > 0)) {
    progress = fromtime / totime;
    if ((progress * 100) > 100) {
      progress = 1.0f;
    }
  }
  return progress;
}
 
- (void)startanimation
{
  cakeyframeanimation *pathanimation = [cakeyframeanimation animationwithkeypath:@"position"];
  pathanimation.calculationmode = kcaanimationpaced;
  pathanimation.fillmode = kcafillmodeforwards;
  pathanimation.removedoncompletion = yes;
  pathanimation.duration = kanimtimeinterval;
  pathanimation.repeatcount = 0;
  pathanimation.autoreverses = yes;
   
  cgmutablepathref arcpath = cgpathcreatemutable();
  cgpathaddpath(arcpath, null, [self bezierpathfromparentlayerarccenter]);
  pathanimation.path = arcpath;
  cgpathrelease(arcpath);
  [self addanimation:pathanimation forkey:@"position"];
}
 
/**
 * 根据父layer获取到一个移动路径
 * @return
 */
- (cgpathref)bezierpathfromparentlayerarccenter
{
  cgfloat centerx = cgrectgetwidth(self.superlayer.frame) / 2.0;
  cgfloat centery = cgrectgetheight(self.superlayer.frame) / 2.0;
  double tmpstartangle = _startangle;
  _startangle = _startangle + (2 * m_pi) * kanimtimeinterval / _animatetime;
  return [uibezierpath bezierpathwitharccenter:cgpointmake(centerx, centery)
                     radius:centerx
                   startangle:tmpstartangle
                    endangle:_startangle
                    clockwise:yes].cgpath;
}
 
- (void)stopanimation
{
  [self removeallanimations];
}
 
- (void)dealloc
{
  [[nsnotificationcenter defaultcenter] removeobserver:self];
}
 
@end
 
/**
 * 圆圈layer
 */
@interface ccprogress : cashapelayer
{
  nstimeinterval _animationtime;
}
 
@property (assign, nonatomic) double initialprogress;
@property (nonatomic) nstimeinterval elapsedtime;                   //已使用时间
@property (assign, nonatomic) double percent;
@property (nonatomic, strong) uicolor *circlecolor;
@property (nonatomic, strong) cashapelayer *progress;
@property (nonatomic, strong) ccprogressthumb *thumb;
@property (nonatomic, assign) cgrect frame;
 
@end
 
@implementation ccprogress
 
- (instancetype) init
{
  if ((self = [super init])) {
    [self setuplayer];
  }
  return self;
}
 
- (void)layoutsublayers
{
  self.path = [self bezierpathwitharccenter];
  self.progress.path = self.path;
   
  self.thumb.frame = cgrectmake((320 - kprogressthumbwh) / 2.0f, 180, kprogressthumbwh, kprogressthumbwh);
  [super layoutsublayers];
}
 
- (void)setuplayer
{
  // 绘制圆
  self.path = [self bezierpathwitharccenter];
  self.fillcolor = [uicolor clearcolor].cgcolor;
  self.strokecolor = [uicolor colorwithred:0.86f green:0.86f blue:0.86f alpha:0.4f].cgcolor;
  self.linewidth = 2;
   
  // 添加可以变动的滚动条
  self.progress = [cashapelayer layer];
  self.progress.path = self.path;
  self.progress.fillcolor = [uicolor clearcolor].cgcolor;
  self.progress.strokecolor = [uicolor whitecolor].cgcolor;
  self.progress.linewidth = 4;
  self.progress.linecap = kcalinecapsquare;
  self.progress.linejoin = kcalinecapsquare;
  [self addsublayer:self.progress];
   
  // 添加可以旋转的thumblayer
  self.thumb = [[ccprogressthumb alloc] init];
  [self addsublayer:self.thumb];
}
 
/**
 * 得到bezier曲线路劲
 * @return
 */
- (cgpathref)bezierpathwitharccenter
{
  cgfloat centerx = cgrectgetwidth(self.frame) / 2.0;
  cgfloat centery = cgrectgetheight(self.frame) / 2.0;
  return [uibezierpath bezierpathwitharccenter:cgpointmake(centerx, centery)
                     radius:centerx
                   startangle:(- m_pi / 2)
                    endangle:(3 * m_pi / 2)
                    clockwise:yes].cgpath;
}
 
- (void)setcirclecolor:(uicolor *)circlecolor
{
  self.progress.strokecolor = circlecolor.cgcolor;
}
 
- (void)setanimtiontime:(nstimeinterval)animtiontime
{
  _animationtime = animtiontime;
  [self.thumb setanimationtime:animtiontime];
}
 
- (void)setelapsedtime:(nstimeinterval)elapsedtime
{
  _initialprogress = [self calculatepercent:_elapsedtime totime:_animationtime];
  _elapsedtime = elapsedtime;
   
  self.progress.strokeend = self.percent;
  [self startanimation];
}
 
- (double)percent
{
  _percent = [self calculatepercent:_elapsedtime totime:_animationtime];
  return _percent;
}
 
- (double)calculatepercent:(nstimeinterval)fromtime totime:(nstimeinterval)totime
{
  double progress = 0.0f;
  if ((totime > 0) && (fromtime > 0)) {
    progress = fromtime / totime;
    if ((progress * 100) > 100) {
      progress = 1.0f;
    }
  }
  return progress;
}
 
- (void)startanimation
{
  cabasicanimation *pathanimation = [cabasicanimation animationwithkeypath:@"strokeend"];
  pathanimation.duration = kanimtimeinterval;
  pathanimation.fromvalue = @(self.initialprogress);
  pathanimation.tovalue = @(self.percent);
  pathanimation.removedoncompletion = yes;
  [self.progress addanimation:pathanimation forkey:nil];
   
  [self.thumb startanimation];
  self.thumb.timelabel.text = [self stringfromtimeinterval:_elapsedtime shortime:yes];
}
 
- (void)stopanimation
{
  _elapsedtime = 0;
  self.progress.strokeend = 0.0;
  [self removeallanimations];
  [self.thumb stopanimation];
}
 
/**
 * 时间格式转换
 * @param interval nstimeinterval
 * @param shorttime bool
 * @return
 */
- (nsstring *)stringfromtimeinterval:(nstimeinterval)interval shortime:(bool)shorttime
{
  nsinteger ti = (nsinteger)interval;
  nsinteger seconds = ti % 60;
  nsinteger minutes = (ti / 60) % 60;
  nsinteger hours = (ti / 3600);
  if (shorttime) {
    return [nsstring stringwithformat:@"%02ld:%02ld", (long)hours, (long)seconds];
  } else {
    return [nsstring stringwithformat:@"%02ld:%02ld:%02ld", (long)hours, (long)minutes, (long)seconds];
  }
}
 
@end
 
@interface ccprogressview ()
 
@property (nonatomic, strong) ccprogress *progresslayer;
@property (nonatomic, strong) nstimer *timer;
 
@end
 
@implementation ccprogressview
 
- (instancetype)init
{
  if ((self = [super init])) {
    [self setupview];
  }
  return self;
}
 
- (instancetype)initwithframe:(cgrect)frame
{
  if ((self = [super initwithframe:frame])) {
    [self setupview];
  }
  return self;
}
 
- (void)layoutsubviews
{
  [super layoutsubviews];
  self.progresslayer.frame = self.bounds;
   
  [self.centerlabel sizetofit];
  self.centerlabel.center = cgpointmake(self.center.x - self.frame.origin.x, self.center.y- self.frame.origin.y);
}
 
- (void)setupview
{
  self.backgroundcolor = [uicolor clearcolor];
  self.clipstobounds = false;
   
  self.progresslayer = [[ccprogress alloc] init];
  self.progresslayer.frame = self.bounds;
  [self.layer addsublayer:self.progresslayer];
   
  _centerlabel = [[uilabel alloc] initwithframe:self.bounds];
  _centerlabel.font = [uifont systemfontofsize:18];
  _centerlabel.textalignment = nstextalignmentcenter;
  _centerlabel.textcolor = [uicolor whitecolor];
  _centerlabel.text = @"已推送至 3 家";
  [self.layer addsublayer:_centerlabel.layer];
}
 
- (void)setanimationtime:(nstimeinterval)animationtime
{
  _animationtime = animationtime;
  [self.progresslayer setanimtiontime:animationtime];
}
 
- (void)startanimation
{
  if (!_timer) {
    _timer = [nstimer timerwithtimeinterval:kanimtimeinterval target:self selector:@selector(dotimerschedule) userinfo:nil repeats:yes];
    [[nsrunloop currentrunloop] addtimer:_timer formode:nsrunloopcommonmodes];
  }
  self.progresslayer.elapsedtime = 0;
  if (_start) _start();
}
 
- (void)dotimerschedule
{
  self.progresslayer.elapsedtime = self.progresslayer.elapsedtime + kanimtimeinterval;;
  if (_animing) _animing(self.progresslayer.elapsedtime);
   
  if (self.progresslayer.elapsedtime >= _animationtime) {
    [self stopanimation];
  }
}
 
- (void)stopanimation
{
  if (_stop) _stop();
  if (_timer) {
    [_timer invalidate];
    _timer = nil;
  }
  [_progresslayer stopanimation];
}
 
@end
 
使用实例
_progressview = [[ccprogressview alloc] initwithframe:cgrectmake((app_width - 240) / 2.0f, (app_height - 240) / 2.0f, 240, 240)];
  [_progressview setanimationtime:60];
  _progressview.start = ^() {
    nslog(@"开始");
  };
  _progressview.animing = ^ (nstimeinterval currenttime) {
    nslog(@"进行中");
  };
  __block id weakself = self;
  _progressview.stop = ^ () {
    nslog(@"结束");
    [weakself dismiss];
  };
  [self addsubview:_progressview];
   
  [_progressview startanimation];

以上所述就是本文的全部内容了,希望大家能够喜欢

延伸 · 阅读

精彩推荐
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01