国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

利用CALayer動畫實現漸變進度條

2019-11-14 19:44:42
字體:
來源:轉載
供稿:網友

今天是第一次寫博客,希望在今后的日子里大家可以共同進步提高。閑話不多說了,首先看一下效果圖。

 

 

 

  其實這個空間是比較簡單的調用CALayer動畫的空間,首先我們做一下簡單的分析。這個動畫的組成分為兩部分,第一部分為填充部分,即“strokeEnd”,另一部分則是填充顏色,即“stroke color”。有這兩個動畫組成了一個CALayer的組動畫。

  實現原理大致明確我們來用代碼實現一下,我們先修改一下它的初始化方法:

-(instancetype)initWithFrameAndPRosse:(CGRect)frame prosseInfo:(ProssenInfo)prosseInfo

{

    self = [super initWithFrame:frame];

    if(self)

    {

        _prossenInfo             = prosseInfo;

       

        [self setCAShapeLayer];

    }

    return self;

}

  這里面我將所需的一部分關于CAShapeLayer的參數寫成了一個結構體,方便使用和查找,接下來我們初始化一下CAShapeLayer這個屬性:

-(void)setCAShapeLayer

{

    _prosseShape                 = [CAShapeLayer layer];

    _prosseShape.strokeColor     = _prossenInfo.lineColor;

    _prosseShape.lineWidth       = _prossenInfo.width;

    _prosseShape.strokeStart     = 0;

    _prosseShape.strokeEnd       = 0;

    _prosseShape.path            = [self prossePath];

    _prosseShape.lineJoin        = kCALineJoinRound;

    _prosseShape.lineCap         = kCALineCapRound;

    _prosseShape.beginTime       =0.1f;

    

    [self.layer addSublayer:_prosseShape];

}這里我們需要注意

   _prosseShape.lineJoin        = kCALineJoinRound;

    _prosseShape.lineCap         = kCALineCapRound;這兩句話,如果不加上的話實現出來的進度條就不是圓角的。

我們改如何給路徑呢?

-(AnimationPoint)animationPoint

{

   

    CGFloat startPoint_x    = self.frame.size.width*(_prossenInfo.minLength<_prossenInfo.maxLength?_prossenInfo.minLength:_prossenInfo.maxLength);

    CGFloat endPoint_x      = self.frame.size.width*(_prossenInfo.minLength>_prossenInfo.maxLength?_prossenInfo.minLength:_prossenInfo.maxLength);

    

    AnimationPoint point    = AnimationPointMake/

                              (CGPointMake(startPoint_x, self.frame.size.height/2.f-_prossenInfo.width/2.f), /

                               CGPointMake(endPoint_x, self.frame.size.height/2.f-_prossenInfo.width/2.f));

    

    return point;

 

}

 

 

-(CGMutablePathRef)prossePath

{

    AnimationPoint point    = [self animationPoint];

    CGMutablePathRef pathRef=CGPathCreateMutable();

    

    CGPathMoveToPoint(pathRef, NULL,point.startPoint.x, point.startPoint.y);

    

    CGPathAddLineToPoint(pathRef, NULL, point.endPoint.x,point.endPoint.y);

   

    CGPathCloseSubpath(pathRef);

   

    return pathRef;

}

我們給定了這個Layer的起始填充部分,顏色,寬度以及路徑,接下來我們開始著手關于它的動畫。

-(void)starAnimation:(NSArray *)colorArray time:(NSTimeInterval)time

{

   __block BOOL colors = NO;

    __weak CAShapeLayer *shape = _prosseShape;

    __block NSMutableArray *animationArray = [NSMutableArray array];

    _aniTime = time;

    

    [shape animateKey:@"strokeEnd"

                   fromValue:[NSNumber numberWithFloat:0.f]

                     toValue:[NSNumber numberWithFloat:0.5f]

                   customize:^BOOL(CABasicAnimation *animation) {

                       animation.duration = time;

                       if (colorArray != nil) {

                           colors  = YES;

                           [animationArray addObject:animation];

                           [shape keyAnimation:@"strokeColor"

                                            keyValues:colorArray

                                            customize:^BOOL(CAKeyframeAnimation *animation) {

                                                animation.duration = time;

                                                 animation.calculationMode = kCAAnimationPaced;

                                                [animationArray addObject:animation];

                                                return colors;

                                            }];

                           [shape animationGroupWithDuration:time

                                                         animations:animationArray

                                                          customize:^(CAAnimationGroup *animation) {

                                                             

                                                          }];

                       }

                       else

                       {

                           colors  = NO;

                           animation.delegate = self;

                       }

                       

                       return colors;

                   }];

}

  因為我們要實現的事漸變顏色的動畫效果,所以我們需要給定兩個參數,一個是顏色數組,一個是進度時間。(當然在實際的下載過程中我們是不知道進度時間的,所以這里本人封裝的控件還不具備很好的完整性,但是我們可以通過修改 toValue:[NSNumber numberWithFloat:0.5f]這個函數參數來實現下載進度,本人會在下篇博客之前修改完畢)。接下來我們實現一下關于動畫時間的計算,因為CALayer動畫本來是不支持動畫時間監聽的,所以這里面我用了一個定時器。

-(void)animationDidStart:(CAAnimation *)anim

{

    _animationTimer  = [NSTimer scheduledTimerWithTimeInterval:AnimationTimer target:self selector:@selector(progressDidStart:) userInfo:nil repeats:YES];

    

}

-(void)progressDidStart:(NSTimer *)timer

{

    if (_delegate &&[_delegate respondsToSelector:@selector(prosseToPoint:withDot:)]) {

        animationProgress += 1/(_aniTime/AnimationTimer);

        if (animationProgress >= 1.0) {

            animationProgress = 1.0;

        }

        [_delegate prosseToPoint:CGPointZero withDot:animationProgress];

        

    }

}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

{

 

    if (_delegate &&[_delegate respondsToSelector:@selector(prosseToPoint:withDot:)]) {

        animationProgress += 1/(_aniTime/AnimationTimer);

        if (animationProgress >= 1.0) {

            animationProgress = 1.0;

        }

        [_delegate prosseToPoint:CGPointZero withDot:animationProgress];

        

    }

    animationProgress = 0;

    [_animationTimer invalidate];

    _animationTimer = nil;

}

  這樣我們一個簡單的進度條控件就寫好了,下面我們來調用一下:

    _proView = [[XDProsseView alloc]initWithFrameAndProsse:CGRectMake(10, 320, self.view.bounds.size.width-20, 50) prosseInfo:ProssenInfoMake(5,1.f , 0.0, [UIColor blueColor].CGColor)];

    _proView.delegate = self;

    [self.view addSubview:_proView];

初始化這個進度條,

   [_proView starAnimation:[NSArray arrayWithObjects:(id)[UIColor greenColor].CGColor,(id)[UIColor redColor].CGColor,(id)[UIColor blackColor].CGColor, nil] time:10.234f];

設置開始,時間,顏色等。

-(void)prosseToPoint:(CGPoint)point withDot:(double)dot

{

NSLog(@"監聽進度條進度%@ %ld",NSStringFromCGPoint(point),dot);

}

第一次寫博客,如果有什么不妥的地方,希望大家及時提出來,我一定會積極改正,謝謝!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 农安县| 灵宝市| 垣曲县| 宝兴县| 乐安县| 鸡东县| 江北区| 新化县| 阜新| 正蓝旗| 中宁县| 错那县| 定边县| 个旧市| 玉溪市| 山东省| 四会市| 昌邑市| 宜兴市| 聂拉木县| 肥东县| 江门市| 靖宇县| 昌都县| 阿鲁科尔沁旗| 环江| 海兴县| 阳曲县| 东乌珠穆沁旗| 明光市| 孝昌县| 微山县| 连山| 玉溪市| 涡阳县| 包头市| 麟游县| 滦平县| 滦平县| 汾阳市| 会理县|