2019-04-03

import "JDTView.h"

@interface JDTView()
//進(jìn)度圓環(huán)
@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@property (nonatomic, strong)NSTimer *timer;


@end

static CGFloat endFloat;

@implementation JDTView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundColor:[UIColor whiteColor]];
    }
    [self initUI];
    return self;
}

- (void)initUI{
//    [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width / 2, self.height / 2) radius:(self.width - 20)/2 startAngle:M_PI / 4 + M_PI / 2 endAngle:M_PI / 4 clockwise:YES]
    
    UIBezierPath * bezier = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:self.frame.size.width/2 startAngle:0  endAngle:M_PI * 2  clockwise:YES];
    [[UIColor blackColor]set];
    bezier.lineWidth = 2;
    [bezier stroke];
    
    CAShapeLayer *bgLayer = [CAShapeLayer layer];
    bgLayer.frame = self.bounds;
    bgLayer.fillColor = [UIColor clearColor].CGColor;//填充色 -  透明
    bgLayer.lineWidth = 20.f;
    bgLayer.strokeColor = [UIColor groupTableViewBackgroundColor].CGColor;//線條顏色
    bgLayer.strokeStart = 0;//起始點(diǎn)
    bgLayer.strokeEnd = 1;//終點(diǎn)
//    bgLayer.lineCap = kCALineCapRound;//讓線兩端是圓滑的狀態(tài)
    bgLayer.path = bezier.CGPath;//這里就是把背景的路徑設(shè)為之前貝塞爾曲線的那個路徑
    [self.layer addSublayer:bgLayer];
    
    
    _shapeLayer = [CAShapeLayer layer];
    _shapeLayer.frame = self.bounds;
    _shapeLayer.fillColor = [UIColor clearColor].CGColor;
    _shapeLayer.lineWidth = 20.0f;
    _shapeLayer.lineCap = kCALineCapRound;
    _shapeLayer.strokeColor = [UIColor blueColor].CGColor;
    _shapeLayer.strokeStart = 0;
    _shapeLayer.strokeEnd = 0;
    _shapeLayer.path = bezier.CGPath;
    [self.layer addSublayer:_shapeLayer];
    
}

- (void)animateToProgress:(CGFloat)progress{
    
//    if(_shapeLayer.strokeEnd != 0){
//        [self animateToZero];
//    }
    
    __weak typeof(self)weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_shapeLayer.strokeEnd * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [weakSelf deleteTimer];
        
        NSString *progressStr = [NSString stringWithFormat:@"%lf",progress];
        NSDictionary *userInfo = @{@"progressStr":progressStr};
        
        weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:weakSelf selector:@selector(animate:) userInfo:userInfo repeats:YES];
    });
}

- (void)animate:(NSTimer *)time{
    
    CGFloat progress = [[time.userInfo objectForKey:@"progressStr"]  floatValue];
    
    if(_shapeLayer.strokeEnd <= progress){
        _shapeLayer.strokeEnd += 0.01;
    }else{
        [self deleteTimer];
    }
}


//暫停
- (void)animateToZero{
    //獲取暫停時當(dāng)前的 endFloat
    endFloat = _shapeLayer.strokeEnd;
    
    [self deleteTimer];
    
    NSString *progressStr = [NSString stringWithFormat:@"%lf",endFloat];
    NSDictionary *userInfo = @{@"endStr":progressStr};
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animateReset:) userInfo:userInfo repeats:YES];
}


- (void)animateReset:(NSTimer *)time{
    if(_shapeLayer.strokeEnd > 0){
        _shapeLayer.strokeEnd = [[time.userInfo objectForKey:@"endStr"]  floatValue];
    }else{
        [self deleteTimer];
    }
}

- (void)deleteTimer{
    [self.timer invalidate];
    self.timer = nil;
}


@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末碴裙,一起剝皮案震驚了整個濱河市睡扬,隨后出現(xiàn)的幾起案子哮兰,更是在濱河造成了極大的恐慌静暂,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件腾它,死亡現(xiàn)場離奇詭異蛔添,居然都是意外死亡章蚣,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進(jìn)店門蜻懦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來甜癞,“玉大人,你說我怎么就攤上這事宛乃∮圃郏” “怎么了?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵烤惊,是天一觀的道長乔煞。 經(jīng)常有香客問我,道長柒室,這世上最難降的妖魔是什么渡贾? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮雄右,結(jié)果婚禮上空骚,老公的妹妹穿的比我還像新娘纺讲。我一直安慰自己,他們只是感情好囤屹,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布熬甚。 她就那樣靜靜地躺著,像睡著了一般肋坚。 火紅的嫁衣襯著肌膚如雪乡括。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天智厌,我揣著相機(jī)與錄音诲泌,去河邊找鬼。 笑死铣鹏,一個胖子當(dāng)著我的面吹牛敷扫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播诚卸,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼葵第,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了合溺?” 一聲冷哼從身側(cè)響起卒密,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎棠赛,沒想到半個月后栅受,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡恭朗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年屏镊,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片痰腮。...
    茶點(diǎn)故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡而芥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出膀值,到底是詐尸還是另有隱情棍丐,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布沧踏,位于F島的核電站歌逢,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏翘狱。R本人自食惡果不足惜秘案,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧阱高,春花似錦赚导、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至未舟,卻和暖如春圈暗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背裕膀。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工厂置, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人魂角。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像智绸,于是被迫代替她去往敵國和親野揪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內(nèi)容