自定义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) - radius, radius, (float)M_PI / 2, (float)M_PI, 0);

// 开始绘制左上角圆弧

CGContextAddArc(context,CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect) + radius, radius, (float)M_PI, 3 * (float)M_PI / 2, 0);

//CGContextClosePath(context);// 关闭路径

CGContextFillPath(context);// 填充路径,该函数自动关闭路径

}

}

#pragma mark -

-(void)dealloc{

[maskView release];

[indicator release];

[label release];

[super dealloc];

}

#pragma mark Action

-(void)show:(BOOL)block{

if (block && blocked==NO) {

CGPoint offset=self.frame.origin;

// 改变视图大小为父视图大小

self.frame=rectSuper;

viewHud.frame=CGRectOffset(viewHud.frame, offset.x, offset.y);

if (maskView==nil) {

maskView=[[UIView alloc]initWithFrame:rectSuper];

}else{

maskView.frame=rectSuper;

}

maskView.backgroundColor=[UIColor clearColor];

[self addSubview:maskView];

[self bringSubviewToFront:maskView];

blocked=YES;

}

[indicator startAnimating];

[self setHidden:NO];

[self setNeedsLayout];

visible=YES;

}

-(void)hide{

visible=NO;

[indicator stopAnimating];

[self setHidden:YES];

}

-(void)setMessage:(NSString*)newMsg{

label.text=newMsg;

}

-(void)alignToCenter{

CGPoint centerSuper={rectSuper.size.width/2,rectSuper.size.height/2};

CGPoint centerSelf={self.frame.origin.x+self.frame.size.width/2,

self.frame.origin.y+self.frame.size.height/2};

CGPoint offset={centerSuper.x-centerSelf.x,centerSuper.y-centerSelf.y};

CGRect newRect=CGRectOffset(self.frame, offset.x, offset.y);

[self setFrame:newRect];

rectHud=newRect;

// NSLog(@"newRect:%f,%f",newRect.origin.x,newRect.origin.y);

}

@end

使用它非常简单,不会比原来更麻烦:

#import <UIKit/UIKit.h>

#import "MyProgressView.h"

@interface RootVC :UIViewController {

MyProgressView* indicator;

}

-(IBAction)btnClicked;

@end

#import "RootVC.h"

@implementation RootVC

-(IBAction)btnClicked{

NSLog(@"%s",__FUNCTION__);

if (indicator.visible==NO) {

[indicator show:NO];

}else {

[indicator hide];

}

}

- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil {

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

}

return self;

}

- (void)viewDidLoad {

[super viewDidLoad];

indicator=[[MyProgressView alloc]initWithFrame:

CGRectMake(0, 0, 120, 120) superView:self.view];

//[indicatoralignToCenter];

}

- (void)viewDidUnload {

[super viewDidUnload];

// Release any retained subviews ofthe main view.

// e.g. self.myOutlet = nil;

}

- (void)dealloc {

[indicator release];

[super dealloc];

}

@end

show方法有一个BOOL值参数,如果你设置为YES,在无敌风火轮出现的同时,父视图界面会被冻结,用户将不能操作任何控件——我想这是我需要的。当hide方法被调用,风火轮消失,界面冻结被取消。一切就这么简单:

点击打开链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值