iOS 用贝塞尔曲线绘制电池

效果图:

Simulator Screen Shot - iPhone 8 Plus - 2018-01-26 at 11.03.56.png

//画电池
    UIBezierPath *path1 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x, y, w, h) cornerRadius:2];
    CAShapeLayer *batteryLayer = [CAShapeLayer layer];
    batteryLayer.lineWidth = lineW;
    batteryLayer.strokeColor = [UIColor blackColor].CGColor;
    batteryLayer.fillColor = [UIColor clearColor].CGColor;
    batteryLayer.path = [path1 CGPath];
    [self.view.layer addSublayer:batteryLayer];
    
    UIBezierPath *path2 = [UIBezierPath bezierPath];
    [path2 moveToPoint:CGPointMake(x+w+1, y+h/3)];
    [path2 addLineToPoint:CGPointMake(x+w+1, y+h*2/3)];
    CAShapeLayer *layer2 = [CAShapeLayer layer];
    layer2.lineWidth = 2;
    layer2.strokeColor = [UIColor blackColor].CGColor;
    layer2.fillColor = [UIColor clearColor].CGColor;
    layer2.path = [path2 CGPath];
    [self.view.layer addSublayer:layer2];

//绘制进度
    batteryView = [[UIView alloc]initWithFrame:CGRectMake(x,y+lineW, 0, h-lineW*2)];
    batteryView.layer.cornerRadius = 2;
    batteryView.backgroundColor = [UIColor colorWithRed:0.324 green:0.941 blue:0.413 alpha:1.000];
    [self.view addSubview:batteryView];

使用的时候直接调用runProgress方法,progressValue为当前电量

- (void)runProgress:(NSInteger)progressValue{
    
    [UIView animateWithDuration:1 animations:^{
        CGRect frame = batteryView.frame;
        frame.size.width = (progressValue*(w-lineW*2))/100;
        batteryView.frame  = frame;
        batteryLabel.text = [[NSString stringWithFormat:@"%ld",(long)progressValue] stringByAppendingString:@"%"];
        
        if (progressValue<10) {
            batteryView.backgroundColor = [UIColor redColor];
        }else{
          batteryView.backgroundColor = [UIColor colorWithRed:0.324 green:0.941 blue:0.413 alpha:1.000];
        }
    }];
}

demo地址:https://github.com/chenmengdi/UIBezierPath-

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,569评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,257评论 4 61
  • 关东野客说:“一辈子很长,要找个懂你的人在一起”。 最近在微博看到一句话说: “到了这个年纪,想找个懂你的人太难,...
    顾闵阅读 685评论 0 2
  • 自制力会消耗人 哪怕是那些具有极强自制力的人也有精疲力竭的时候。如果每天面临很多压力,无论是谁都会感到乏力,无法达...
    木鱼之眼阅读 332评论 0 0