custom progress bar for iphone&ipad(转)

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:[url]http://www.naveenshan.yolasite.com/blog/custom-slider-for-iphone-ipad[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值