IOS-battery icon [control battery display with the beiser curve], ios-beiser

Source: Internet
Author: User

IOS-battery icon [control battery display with the beiser curve], ios-beiser

 

Based on the UIView class: 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 () // batterwidth @ property (nonatomic, assign) CGFloat B _width; // battery height @ property (nonatomic, assign) CGFloat B _height; // outside battery width @ property (nonatomic, assign) CGFloat B _lineW; @ property (nonatomic, strong) UIView * batteryView; @ end @ implementation threshold-(instancetype) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {[self drawBattery];} return self ;}// draw the icon-(void) drawBattery {// x coordinate CGFloat B _x = 1; // y coordinate CGFloat B _y = 1; _ B _height = self. bounds. size. height-2; _ B _width = self. bounds. size. width-5; _ B _lineW = 1; // draw the battery [left battery] UIBezierPath * pathLeft = [UIBezierPath labels: 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]; // draw a battery [Right battery arrow] 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];} // control power display-(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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.