iOS-電池表徵圖【結合貝茲路徑控制電量顯示】,ios-貝塞爾

來源:互聯網
上載者:User

iOS-電池表徵圖【結合貝茲路徑控制電量顯示】,ios-貝塞爾

 

基於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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.