iOS - Animation之ProgressAnimation

今天主要講一些動畫的運用,也就是進度條,有弧形的,扇形的,圓形的。

其實沒什么難點,我封裝在一個個View里,直接調(diào)用就行,弧度跟著進度條走就OK的。
飲水思源,喜歡或者用得上就給個Star

UIBezierPath方法詳解

UIBezierPath 顧名思義,這是用貝塞爾曲線的方式來構(gòu)建一段弧線,你可以用任意條弧線來組成你想要的形狀

-(void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise
center 圓弧圓心
radius 圓弧半徑
startAngle 開始弧度
endAngle 結(jié)束弧度
closewise 是否順時針

-(void)fill
填充顏色
-(void)stroke
線條顏色

注: 使用UIBezierPath繪畫的代碼寫在自定義視圖的drawRect:方法中

CAShaperLayer

CAShaperLayer也就是在現(xiàn)有的圖層上再次添加或覆蓋一層以達到界面在顯示時會呈現(xiàn)出不同形狀的效果

 fillColor 填充顏色
 strokeColor 邊框顏色
 lineWidth 線條寬度
 path 線條曲線
 strokeStart 開始角度
 strokeEnd 結(jié)束角度

注:進度條主要就以上方法

應(yīng)用界面調(diào)用

懶加載UISlider

- (UISlider *)sectorSlider{
if (_sectorSlider == nil) {
    _sectorSlider = [[UISlider alloc]initWithFrame:CGRectMake(winWidth/2.0 - 100, 100, 200, 10)];
    _sectorSlider.continuous = YES;
    [_sectorSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    [_sectorSlider setMinimumValue:0.0];
    [_sectorSlider setMaximumValue:1.0];
}
return _sectorSlider;
 }

點擊調(diào)用UISlider方法

-(void)sliderValueChanged:(UISlider *)slider{
NSLog(@"---%f", slider.value);
self.percentLable.text = [NSString stringWithFormat:@"%.2f",slider.value * 100];
[self.animationView  setStartMove:slider.value];
[self.animationBallView setStartMove:slider.value];
[self.animationTimeView setStartMove:slider.value];
}

圓形進度條生成

  1. UIBezierPath根據(jù)進度條隨時改變開始弧度和結(jié)束弧度,不設(shè)置中心point

    -(void)drawRect:(CGRect)rect {
    CGPoint point = CGPointMake(100, 100);
    CGFloat radius = 95.0f;
    
    UIBezierPath *ballPath = [UIBezierPath bezierPathWithArcCenter:point radius:radius startAngle:self.startAngle endAngle:self.endAngle clockwise:YES];
    
    [[UIColor greenColor]set];
    [ballPath fill];
    
    //    在球形的外面繪制一個描邊空心的圓形,不然很難看
    UIBezierPath *strokePath = [UIBezierPath bezierPathWithArcCenter:point radius:radius startAngle:0 endAngle:-0.00000001 clockwise:YES];
    [[UIColor lightGrayColor]set];
    [strokePath stroke];
    }
    
    
    -(void)setStartMove:(CGFloat)value{
    
     //    設(shè)置起始點,位置是根據(jù)進度動態(tài)變換的
    self.startAngle = M_PI_2 - value * M_PI;
    self.endAngle = M_PI_2 + value * M_PI;
    
    [self setNeedsDisplay];
    }
    

扇形進度條生成

  1. UIBezierPath根據(jù)進度條隨時改變結(jié)束弧度,設(shè)置中心point

    -(void)drawRect:(CGRect)rect{
    CGPoint point = CGPointMake(100 , 100);
    CGFloat radius = 95.0f;
    CGFloat startAngle = - M_PI_2;
    CGFloat endAngle = startAngle + self.endAngle;
    UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:point radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    [path addLineToPoint:point];
    [[UIColor greenColor]set];
    [path fill];
    UIBezierPath *strokePath = [UIBezierPath bezierPathWithArcCenter:point radius:radius startAngle:0 endAngle:-0.00000001 clockwise:YES];
    [[UIColor lightGrayColor]set];
    [strokePath stroke];
    
    } 
    -(void)setStartMove:(CGFloat)value{
    self.endAngle = value * M_PI * 2;
    [self setNeedsDisplay];
    }
    

弧形進度條生成

  1. 先畫一個封閉的背景圓self.backShaperLayer

  2. 根據(jù)圓弧先設(shè)置self.shaperLayer的開始,根據(jù)進度條的改變隨時改變結(jié)束弧度

    -(void)drawRect:(CGRect)rect{
    
    self.shapeLayer = [CAShapeLayer layer];
    self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.shapeLayer.lineWidth = 6.0f;
    self.shapeLayer.strokeColor = [UIColor greenColor].CGColor;
    
    
    self.backShapeLayer = [CAShapeLayer layer];
    self.backShapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.backShapeLayer.lineWidth = 6.0f;
    self.backShapeLayer.strokeColor = [UIColor darkGrayColor].CGColor;
    
    
    UIBezierPath * path = [UIBezierPath bezierPath];
    [path addArcWithCenter:CGPointMake(50, 50) radius:40 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    
    self.shapeLayer.path = path.CGPath;
    self.shapeLayer.strokeStart = 0;
    self.shapeLayer.strokeEnd = self.toValue;
    self.backShapeLayer.path = path.CGPath;
    self.backShapeLayer.strokeStart = 0;
    self.backShapeLayer.strokeEnd = 1;
    [self.layer addSublayer:self.backShapeLayer];
    [self.layer addSublayer:self.shapeLayer];
    
    }
    -(void)setStartMove:(CGFloat)value{
    self.toValue = value;
    [self setNeedsDisplay];
    }
    

Dome: github地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • #define kBlackColor [UIColor blackColor] //.h //劃線 + (voi...
    CHADHEA閱讀 855評論 0 1
  • Quartz2D以及drawRect的重繪機制字數(shù)1487 閱讀21 評論1 喜歡1一、什么是Quartz2D Q...
    PurpleWind閱讀 909評論 0 3
  • 18- UIBezierPath官方API中文翻譯(待校對) ----------------- 華麗的分割線 -...
    醉臥欄桿聽雨聲閱讀 1,161評論 1 1
  • UIBezierPath Class Reference 譯:UIBezierPath類封裝了Core Graph...
    鋼鉄俠閱讀 1,942評論 0 3
  • 任務(wù)是對新員工的考驗,可以完成,不成也沒事,換一個人來做。 最重要的是溝通,及時反饋,不行就算了,一直拖延下去沒有...
    瀚海一滴閱讀 251評論 0 0

友情鏈接更多精彩內(nèi)容