最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS使用progssview仿滴滴打車圓形計時

 更新時間:2015年07月31日 10:25:52   投稿:hebedich  
本文給大家分享的是IOS中實現(xiàn)仿滴滴打車的原型計時效果,非常的實用,有需要的小伙伴可以參考下。

實現(xiàn)類似在微信中使用的滴滴打車的progressview,實現(xiàn)效果如圖

//
// 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);
 
/**
 * 動畫結(jié)束
 */
typedef void(^block_progress_stop)();
 
@interface CCProgressView : UIView
{
  NSTimeInterval _animationTime;
}
 
@property (nonatomic, strong) UILabel *centerLabel;    // 中心Label
 
@property (nonatomic, copy) block_progress_start start;   // 動畫開始回調(diào)
@property (nonatomic, copy) block_progress_animing animing; // 動畫進行
@property (nonatomic, copy) block_progress_stop stop;    // 動畫結(jié)束回調(diào)
 
- (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上旋轉(zhuǎn)的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"];
}
 
/**
 * 根據(jù)父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];
   
  // 添加可以旋轉(zhuǎn)的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];
}
 
/**
 * 時間格式轉(zhuǎn)換
 * @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(@"結(jié)束");
    [weakSelf dismiss];
  };
  [self addSubview:_progressView];
   
  [_progressView startAnimation];

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡

相關文章

  • IOS開發(fā)筆記之禁用手勢滑動返回功能的示例

    IOS開發(fā)筆記之禁用手勢滑動返回功能的示例

    本篇文章主要介紹了IOS開發(fā)筆記之禁用手勢滑動返回功能的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • iOS開發(fā)實現(xiàn)HTTPS之cer文件的使用詳解

    iOS開發(fā)實現(xiàn)HTTPS之cer文件的使用詳解

    下面小編就為大家分享一篇iOS開發(fā)實現(xiàn)HTTPS之cer文件的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS指紋識別的簡單應用

    iOS指紋識別的簡單應用

    這篇文章主要為大家詳細介紹了iOS指紋識別的簡單應用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • iOS使用Masonry時進行動畫操作方法

    iOS使用Masonry時進行動畫操作方法

    下面小編就為大家分享一篇iOS使用Masonry時進行動畫操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • iOS中如何判斷當前網(wǎng)絡環(huán)境是2G/3G/4G/5G/WiFi

    iOS中如何判斷當前網(wǎng)絡環(huán)境是2G/3G/4G/5G/WiFi

    這篇文章主要給大家介紹了關于iOS中如何判斷當前網(wǎng)絡環(huán)境是2G/3G/4G/5G/WiFi的相關資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • ios使用AVFoundation讀取二維碼的方法

    ios使用AVFoundation讀取二維碼的方法

    這篇文章主要介紹了ios使用AVFoundation讀取二維碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 淺談WKWebView 在64位設備上的白屏問題

    淺談WKWebView 在64位設備上的白屏問題

    下面小編就為大家?guī)硪黄獪\談WKWebView 在64位設備上的白屏問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • iOS中UIAlertView3秒后消失的兩種實現(xiàn)方法

    iOS中UIAlertView3秒后消失的兩種實現(xiàn)方法

    這篇文章主要介紹了iOS中UIAlertView3秒后消失的兩種實現(xiàn)方法,實現(xiàn)方法涉及到NSTimer和PerformSelector:withObject:afterDelay:方法的結(jié)合使用,需要的朋友可以參考下
    2017-12-12
  • iOS延遲執(zhí)行方法詳解

    iOS延遲執(zhí)行方法詳解

    這篇文章主要為大家詳細介紹了iOS延遲執(zhí)行方法,包括performSelector(NSObject)方法、NSTimer、GCD和sleep(NSThread)四種方法,需要的朋友可以參考下
    2016-11-11
  • iOS開發(fā)————詳解適配iOS10問題

    iOS開發(fā)————詳解適配iOS10問題

    ios10已經(jīng)推出一段時間了,這篇文章主要介紹了iOS開發(fā)————詳解適配iOS10,有興趣的可以了解一下。
    2016-12-12

最新評論

晋江市| 抚远县| 枣阳市| 广德县| 长顺县| 阿鲁科尔沁旗| 四子王旗| 安阳市| 梓潼县| 楚雄市| 渭源县| 平陆县| 杭锦旗| 鸡西市| 仙居县| 漠河县| 乌鲁木齐市| 和平区| 高雄县| 镇安县| 万宁市| 汝南县| 寿光市| 富锦市| 广河县| 黄龙县| 潞西市| 西华县| 乌兰察布市| 抚松县| 迁西县| 梁河县| 法库县| 嘉鱼县| 洪泽县| 巴楚县| 上林县| 金山区| 和政县| 和平区| 调兵山市|