自定义UIActivityIndicator

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 19:39 点击:

苹果无敌风火轮如果不是那么酷的话,我们就不需要定制它了。可惜的是,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) -

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Lexical or Preprocessor Issue 'xxx.h
      • ios学习笔记(二)xcode 4.3.2下实现基本交互
      • ios版本的helloworld
      • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
      • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
      • IOS类似iphone通讯录TableView的完整demo【附源码】
      • UITableView一些方法
      • [iPhone中级]iPhone团购信息客户端的开发 (二)
      • 如何为Iphone应用创建启动界面
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1