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

 更新时间:2015年07月31日 10:25:52   投稿:hebedich  
本文给大家分享的是IOS中实现仿滴滴打车的原型计时效果,非常的实用,有需要的小伙伴可以参考下。

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

//
// 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];

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

相关文章

  • iOS App中UITableView左滑出现删除按钮及其cell的重用

    iOS App中UITableView左滑出现删除按钮及其cell的重用

    这篇文章主要介绍了iOS App中UITableView左滑出现删除按钮及其cell的重用的方法,实例代码为传统的Objective-C语言,需要的朋友可以参考下
    2016-03-03
  • 两行IOS代码实现轮播图

    两行IOS代码实现轮播图

    这篇文章主要为大家详细介绍了两行IOS代码实现轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • IOS开发-多线程队列测试代码

    IOS开发-多线程队列测试代码

    这篇文章中,我不会说多线程是什么、线程和进程的区别、多线程有什么用,当然我也不会说什么是串行、什么是并行等问题,这些我们应该都知道的。
    2017-09-09
  • IOS文件的简单读写实例详解

    IOS文件的简单读写实例详解

    这篇文章主要介绍了IOS文件的简单读写实例详解的相关资料,需要的朋友可以参考下
    2017-07-07
  • 解决Alamofire库在iOS7下设置Head无效的问题

    解决Alamofire库在iOS7下设置Head无效的问题

    本文主要介绍Alamofire库在iOS下设置Head,这里通过代码实例解决不同版本的IOS系统出现的问题,有需要的小伙伴可以参考下
    2016-07-07
  • iOS中UITableview错位的问题怎么修复

    iOS中UITableview错位的问题怎么修复

    这篇文章主要介绍了iOS中UITableview错位的问题以及修复方法,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-01-01
  • 详解iOS游戏开发中Cocos2D的坐标位置关系

    详解iOS游戏开发中Cocos2D的坐标位置关系

    这篇文章主要介绍了iOS游戏开发中Cocos2D的坐标位置关系,Cocos2D是专门用来开发iOS游戏的开源框架,文中示例代码采用Objective-C语言,需要的朋友可以参考下
    2016-02-02
  • Objective-C中字符串NSString的常用操作方法总结

    Objective-C中字符串NSString的常用操作方法总结

    这篇文章主要介绍了Objective-C中字符串NSString的常用操作方法总结,Objective-C中NSString和NSMutableString这两个类下包含了操作字符串的大多数方法,需要的朋友可以参考下
    2016-04-04
  • 解决iOS验证码显示在左边问题

    解决iOS验证码显示在左边问题

    这篇文章主要介绍了iOS验证码显示在左边问题,本文给大家分享解决思路通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • iOS安全防护系列之ptrace反调试与汇编调用系统方法详解

    iOS安全防护系列之ptrace反调试与汇编调用系统方法详解

    这篇文章主要给大家介绍了关于iOS安全防护系列之ptrace反调试与汇编调用系统方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07

最新评论