自定义UIActivityIndicator
苹果无敌风火轮如果不是那么酷的话,我们就不需要定制它了。可惜的是,UIActivityIndicator只有一个初始化方法 initWithActivityIndicatorStyle,我们一不能任意改变它的大小——有时候我们需要一个比 UIActivityIndicatorViewStyleWhiteLarge还要更大的无敌风火轮;二,它不够友好——我们需要在风火轮的下面显示一些友好的提示信息。
为此,我们不惜代价,自己用一个UIActivityIndicator控件和用Quartz绘图的手段,定制了一个自己的无敌风火轮。
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MyProgressView :UIView {
UIActivityIndicatorView* indicator;
UILabel* label;
BOOL visible,blocked;
UIView* maskView;
CGRect rectHud,rectSuper,rectOrigin;//外壳区域、父视图区域
UIView* viewHud;//外壳
}
@property (assign) BOOL visible;
-(id)initWithFrame:(CGRect)frame superView:(UIView*)superView;
-(void)show:(BOOL)block;// block:是否阻塞父视图
-(void)hide;
-(void)setMessage:(NSString*)newMsg;
-(void)alignToCenter;
@end
#import "MyProgressView.h"
@implementation MyProgressView
@synthesize visible;
-(id)initWithFrame:(CGRect)frame superView:(UIView*)superView{
rectOrigin=frame;
rectSuper=[superView bounds];
//保持正方形比例
rectHud=CGRectMake(frame.origin.x,frame.origin.y, frame.size.width, frame.size.width);
self = [super initWithFrame:rectHud];
if (self) {
self.backgroundColor =[UIColor clearColor];
self.opaque = NO;
viewHud=[[UIView alloc]initWithFrame:rectHud];
[self addSubview:viewHud];
indicator=[[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:
UIActivityIndicatorViewStyleWhiteLarge];
double gridUnit=round(rectHud.size.width/12);
float ind_width=6*gridUnit;
indicator.frame=CGRectMake(
3*gridUnit,
2*gridUnit,
ind_width,
ind_width);
[viewHud addSubview:indicator];
CGRect rectLabel=CGRectMake(1*gridUnit,
9*gridUnit,
10*gridUnit, 2*gridUnit);
label=[[UILabel alloc]initWithFrame:rectLabel];
label.backgroundColor=[UIColor clearColor];
label.font=[UIFont fontWithName:@"Arial" size:14];
label.textAlignment=UITextAlignmentCenter;
label.textColor=[UIColor whiteColor];
label.text=@"请等待...";
label.adjustsFontSizeToFitWidth=YES;
[viewHud addSubview:label];
visible=NO;
[self setHidden:YES];
[superViewaddSubview:self];
}
return self;
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(CGRect)rect {
if(visible){
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect boxRect = rectHud;
// 绘制圆角矩形
float radius = 10.0f;
// 路径开始
CGContextBeginPath(context);
// 填充色:灰度0.0,透明度:0.1
CGContextSetGrayFillColor(context,0.0f, 0.25);
// 画笔移动到左上角的圆弧处
CGContextMoveToPoint(context,CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect));
// 开始绘制右上角圆弧:圆心x坐标,圆心y坐标,起始角,终止角,方向为顺时针
CGContextAddArc(context,CGRectGetMaxX(boxRect) - radius, CGRectGetMinY(boxRect) + radius, radius, 3 * (float)M_PI / 2, 0, 0);
// 开始绘制右下角圆弧
CGContextAddArc(context,CGRectGetMaxX(boxRect) - radius, CGRectGetMaxY(boxRect) - radius, radius, 0, (float)M_PI / 2, 0);
// 开始绘制左下角圆弧
CGContextAddArc(context,CGRectGetMinX(boxRect) + radius, CGRectGetMaxY(boxRect) -
相关新闻>>
- 发表评论
-
- 最新评论 更多>>