Wednesday, 10 April 2013

UICircularProgress Bar for iOS

CircularProgress.h


// UICircularProgress.h
// CircularProgress
//
// Created by Prince Kumar Sharma on 30/03/13.
// Copyright (c) 2013 Prince Kumar Sharma. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
typedef enum
{
CPDirectionClockWise=0,
CPDirectionAnticlockWise=1
}CPDirection;
@interface UICircularProgress : UIView
{
int __radius;
int __strokeWidth;
CPDirection __direction;
UILabel *displayLabel;
}
@property(nonatomic,strong)UIColor *_strokeColor;
@property(nonatomic,assign) int _angle;
@property(nonatomic,strong)UIColor *_centerBackground;
@property(nonatomic,strong)UIColor *_strokeBackground;
-(id)initWithFrame:(CGRect)frame StrokeWidth:(int)strokeWidth AndDirection:(CPDirection)direction;
@end


CircularProgress.m



//
// UICircularProgress.m
// CircularProgress
//
// Created by Prince Kumar Sharma on 30/03/13.
// Copyright (c) 2013 Prince Kumar Sharma. All rights reserved.
//
#import "UICircularProgress.h"
#define DegreetoRadian(deg) ( (M_PI * (deg)) / 180.0 )
#define RadiantoDegree(rad) ( (180.0 * (rad)) / M_PI )
@implementation UICircularProgress
-(id)initWithFrame:(CGRect)frame StrokeWidth:(int)strokeWidth AndDirection:(CPDirection)direction
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
__centerBackground=[UIColor brownColor];
__strokeBackground=[UIColor whiteColor];
__strokeColor=[UIColor darkGrayColor];
self.backgroundColor=[UIColor clearColor];
__strokeWidth=strokeWidth;
__radius=frame.size.width/2-strokeWidth;
__direction=direction;
__angle=360;
displayLabel=[[UILabel alloc] initWithFrame:CGRectMake(0+__strokeWidth, 0+__strokeWidth,frame.size.width-__strokeWidth*2, frame.size.width-__strokeWidth*2)];
[displayLabel setBackgroundColor:[UIColor cyanColor]];
[displayLabel setText:[NSString stringWithFormat:@"%i°",__angle]];
[displayLabel setTextAlignment:NSTextAlignmentCenter];
[displayLabel setFont:[UIFont boldSystemFontOfSize:14.0f]];
[displayLabel.layer setCornerRadius:frame.size.width/2-__strokeWidth];
[self customSettings];
[self addSubview:displayLabel];
}
return self;
}
-(void)set_angle:(int)_angle
{
if (__angle!=_angle) {
__angle=_angle;
if (_angle<0) {
__angle=360;
}
if (_angle>360) {
__angle=0;
}
}
[displayLabel setText:[NSString stringWithFormat:@"%i°",__angle]];
[self setNeedsDisplay];
}
-(void)set_strokeColor:(UIColor *)_strokeColor
{
if (__strokeColor!=_strokeColor) {
__strokeColor=_strokeColor;
}
[self setNeedsDisplay];
}
-(void)set_centerBackground:(UIColor *)_centerBackground
{
if (__centerBackground!=_centerBackground) {
__centerBackground=_centerBackground;
}
[self setNeedsDisplay];
}
-(void)set_strokeBackground:(UIColor *)_strokeBackground
{
if (__strokeBackground!=_strokeBackground) {
__strokeBackground=_strokeBackground;
}
[self setNeedsDisplay];
}
-(void)customSettings
{
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
[gradient setCornerRadius:self.frame.size.width/2];
gradient.colors=[NSArray arrayWithObjects:(id)__strokeBackground.CGColor,(id)[UIColor darkGrayColor].CGColor,nil];
[gradient setOpaque:YES];
UIGraphicsBeginImageContext(gradient.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[gradient renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[gradient setFrame:displayLabel.bounds];
[gradient setCornerRadius:self.frame.size.width/2-__strokeWidth];
[gradient setColors:[NSArray arrayWithObjects:(id)__centerBackground.CGColor,(id)[UIColor darkGrayColor].CGColor,nil]];
UIGraphicsBeginImageContext(gradient.bounds.size);
CGContextRef context2 = UIGraphicsGetCurrentContext();
[gradient renderInContext:context2];
UIImage *cenImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setBackgroundColor:[UIColor colorWithPatternImage:img]];
[displayLabel setBackgroundColor:[UIColor colorWithPatternImage:cenImg]];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,__strokeWidth);
CGContextSetStrokeColorWithColor(context, __strokeColor.CGColor);
CGContextMoveToPoint(context,self.frame.size.width-__strokeWidth/2, self.frame.size.width/2);
CGContextAddArc(context,self.frame.size.width/2,self.frame.size.width/2,self.frame.size.width/2-__strokeWidth/2, DegreetoRadian(0),DegreetoRadian(__angle),__direction);
CGContextStrokePath(context);
[self customSettings];
// Drawing code
}
@end


View Controller Containing code to use it

// CPViewController.m
// CircularProgress
//
// Created by Prince Kumar Sharma on 30/03/13.
// Copyright (c) 2013 Prince Kumar Sharma. All rights reserved.
#import "CPViewController.h"
#import "UICircularProgress.h"
@interface CPViewController ()
@end
@implementation CPViewController
- (void)viewDidLoad
{
UICircularProgress *progress=[[UICircularProgress alloc] initWithFrame:CGRectMake(20, 20, 50, 50) StrokeWidth:10 AndDirection:CPDirectionAnticlockWise];
[progress set_centerBackground:[UIColor yellowColor]];
[progress set_angle:50];
UICircularProgress *progress2=[[UICircularProgress alloc] initWithFrame:CGRectMake(50, 100, 100, 100) StrokeWidth:20 AndDirection:CPDirectionClockWise];
[progress2 set_angle:90];
[progress2 set_centerBackground:[UIColor cyanColor]];
[progress2 set_strokeBackground:[UIColor blueColor]];
[progress2 set_strokeColor:[UIColor purpleColor]];
[self.view addSubview:progress];
[self.view addSubview:progress2];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

No comments:

Post a Comment