Posted by NaveenShan on Wednesday, November 3, 2010 Under: iPhone/iPad
/**************************************************************************************
/* File Name : ProgressBar.h
/* Project Name : <nil> Generic
/* Description : A Custom Progress Bar View
/* Version : 1.0
/* Created by : Naveen Shan
/* Created on : 13/10/10
/* Copyright (C) 2010 RapidValue IT Services Pvt. Ltd. All Rights Reserved.
/**************************************************************************************/
#import <UIKit/UIKit.h>
@interface ProgressBar : UIView {
float minValue, maxValue;
float currentValue;
UIColor *lineColor, *progressRemainingColor, *progressColor, *textColor;
}
@property (readwrite) float minValue, maxValue, currentValue;
@property (nonatomic, retain) UIColor *lineColor, *progressRemainingColor, *progressColor, *textColor;
-(void)setNewRect:(CGRect)newFrame;
@end
/**************************************************************************************
/* File Name : ProgressBar.m
/* Project Name : <nil> Generic
/* Description : N/A
/* Version : 1.0
/* Created by : Naveen Shan
/* Created on : 13/10/10
/* Copyright (C) 2010 RapidValue IT Services Pvt. Ltd. All Rights Reserved.
/**************************************************************************************/
#import "ProgressBar.h"
@implementation ProgressBar
#pragma mark -
@synthesize minValue, maxValue, currentValue;
@synthesize lineColor, progressRemainingColor, progressColor, textColor;
#pragma mark -
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
minValue = 0;
maxValue = 1;
currentValue = 0;
self.backgroundColor = [UIColor clearColor];
lineColor = [[UIColor whiteColor] retain];
textColor = [[UIColor magentaColor] retain];
progressColor = [[UIColor darkGrayColor] retain];
progressRemainingColor = [[UIColor lightGrayColor] retain];
}
return self;
}
#pragma mark -
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 3);
CGContextSetStrokeColorWithColor(context,[lineColor CGColor]);
//upper half
CGContextSetFillColorWithColor(context, [[progressRemainingColor colorWithAlphaComponent:.7] CGColor]);
CGContextAddRect(context, CGRectMake(2, 2, rect.size.width-4, ((rect.size.height/2)-2)));
CGContextFillPath(context);
//lower half
CGContextSetFillColorWithColor(context, [progressRemainingColor CGColor]);
CGContextAddRect(context, CGRectMake(2, rect.size.height/2-2, rect.size.width-4, ((rect.size.height/2)-2)));
CGContextFillPath(context);
//border
CGContextAddRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height-2));
CGContextStrokePath(context);
//to plot progress
float amount = (currentValue/(maxValue - minValue)) * (rect.size.width-5);
CGContextSetFillColorWithColor(context, [progressColor CGColor]);
CGContextAddRect(context, CGRectMake(2,2, amount, rect.size.height-5));
CGContextFillPath(context);
//to draw percentage text
CGContextSetFillColorWithColor(context, [textColor CGColor]);
CGContextSelectFont(context, "Helvetica", rect.size.height/2, kCGEncodingMacRoman);
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
CGContextSetTextDrawingMode(context, kCGTextFill);
char str[20];
sprintf(str, "%d %s" ,(int)currentValue,"%");
CGContextShowTextAtPoint(context,((rect.size.width/2)-10),rect.size.height/2,str,strlen(str));
}
-(void)setNewRect:(CGRect)newFrame {
self.frame = newFrame;
[self setNeedsDisplay];
}
#pragma mark -
-(void)setMinValue:(float)newMin {
minValue = newMin;
[self setNeedsDisplay];
}
-(void)setMaxValue:(float)newMax {
maxValue = newMax;
[self setNeedsDisplay];
}
-(void)setCurrentValue:(float)newValue {
if(newValue < minValue)
currentValue = minValue;
else if(newValue > maxValue)
currentValue = maxValue;
else
currentValue = newValue;
[self setNeedsDisplay];
}
#pragma mark -
-(void)setLineColor:(UIColor *)newColor {
[newColor retain];
[lineColor release];
lineColor = newColor;
[self setNeedsDisplay];
}
-(void)setTextColor:(UIColor *)newColor {
[newColor retain];
[textColor release];
textColor = newColor;
[self setNeedsDisplay];
}
-(void)setProgressColor:(UIColor *)newColor {
[newColor retain];
[progressColor release];
progressColor = newColor;
[self setNeedsDisplay];
}
-(void)setProgressRemainingColor:(UIColor *)newColor {
[newColor retain];
[progressRemainingColor release];
progressRemainingColor = newColor;
[self setNeedsDisplay];
}
#pragma mark -
- (void)dealloc {
[lineColor release];
[textColor release];
[progressColor release];
[progressRemainingColor release];
[super dealloc];
}
Call for Custom Progress Bar.
First, Implement the above code as new File
And Call like following,
ProgressBar *objProgressBar= [[ProgressBar alloc] initWithFrame:CGRectMake(100,100,600,75)];
objProgressBar.maxValue=100.0;
objProgressBar.minValue=0.0;
objProgressBar.lineColor=[UIColor redColor];
objProgressBar.textColor=[UIColor magentaColor];
objProgressBar.progressColor=[UIColor greenColor];
objProgressBar.progressRemainingColor=[UIColor blueColor];
objProgressBar.currentValue=10.0;
[self.view addSubview:objProgressBar];
[objProgressBar release];
From:
http://www.naveenshan.yolasite.com/blog/custom-slider-for-iphone-ipad