当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS-电池图标【结合贝塞尔曲线控制电量显示】

iOS-电池图标【结合贝塞尔曲线控制电量显示】

2018年03月02日  | 移动技术网IT编程  | 我要评论

网页传奇哪个好玩,电工电子,饲料添加剂网

 

基于UIView类:WKJBatteryView

WKJBatteryView.h

#import <UIKit/UIKit.h>

@interface WKJBatteryView : UIView
/**
 value:0 - 100
 */
- (void)setBatteryValue:(NSInteger)value;
@end

WKJBatteryView.m

#import "WKJBatteryView.h"
@interface WKJBatteryView()
///电池宽度
@property (nonatomic,assign) CGFloat b_width;
///电池高度
@property (nonatomic,assign) CGFloat b_height;
///电池外线宽
@property (nonatomic,assign) CGFloat b_lineW;
@property (nonatomic,strong) UIView *batteryView;
@end
@implementation WKJBatteryView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self drawBattery];
    }
    return self;
}
///画图标
- (void)drawBattery{
    ///x坐标
    CGFloat b_x = 1;
    ///y坐标
    CGFloat b_y = 1;
    _b_height = self.bounds.size.height - 2;
    _b_width = self.bounds.size.width - 5;
    _b_lineW = 1;
    
    //画电池【左边电池】
    UIBezierPath *pathLeft = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(b_x, b_y, _b_width, _b_height) cornerRadius:2];
    CAShapeLayer *batteryLayer = [CAShapeLayer layer];
    batteryLayer.lineWidth = _b_lineW;
    batteryLayer.strokeColor = [UIColor lightGrayColor].CGColor;
    batteryLayer.fillColor = [UIColor clearColor].CGColor;
    batteryLayer.path = [pathLeft CGPath];
    [self.layer addSublayer:batteryLayer];
    
    //画电池【右边电池箭头】
    UIBezierPath *pathRight = [UIBezierPath bezierPath];
    [pathRight moveToPoint:CGPointMake(b_x + _b_width+1, b_y + _b_height/3)];
    [pathRight addLineToPoint:CGPointMake(b_x + _b_width+1, b_y + _b_height * 2/3)];
    CAShapeLayer *layerRight = [CAShapeLayer layer];
    layerRight.lineWidth = 2;
    layerRight.strokeColor = [UIColor lightGrayColor].CGColor;
    layerRight.fillColor = [UIColor clearColor].CGColor;
    layerRight.path = [pathRight CGPath];
    [self.layer addSublayer:layerRight];
    
    ///电池内填充
    _batteryView = [[UIView alloc]initWithFrame:CGRectMake(b_x + 1,b_y + _b_lineW, 0, _b_height - _b_lineW * 2)];
    _batteryView.layer.cornerRadius = 2;
    _batteryView.backgroundColor = [UIColor colorWithRed:0.324 green:0.941 blue:0.413 alpha:1.000];
    [self addSubview:_batteryView];
}
///控制电量显示
- (void)setBatteryValue:(NSInteger)value{
    if (value<10) {
        _batteryView.backgroundColor = [UIColor redColor];
    }else{
        _batteryView.backgroundColor = [UIColor colorWithRed:0.324 green:0.941 blue:0.413 alpha:1.000];
    }
    
    CGRect rect = _batteryView.frame;
    rect.size.width = (value*(_b_width - _b_lineW * 2))/100;
    _batteryView.frame  = rect;
}
@end
 
封装好WKJBatteryView后,直接 initWithFrame 实例化引用,然后添加到试图上就可以了,下面是效果:
 

 

落日熔金,暮云合璧,人在何处。染柳烟浓,吹梅笛怨,春意知几许。元宵佳节,融和天气,次第岂无风雨。来相召、香车宝马,谢他酒朋诗侣。中州盛日,闺门多暇,记得偏重三五。铺翠冠儿,捻金雪柳,簇带争济楚。如今憔悴,风鬟霜鬓,怕见夜间出去。不如向、帘儿底下,听人笑语。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网