进度显示例子学习


Cocoa Touch提供用于通知用户等待进程完成的类:
1、UIActivityIndicatorView:提供一个旋转的圆,通知用户等待,不提供具体信息
2、UIProgressView:显示进度条,量化工作量(时间)
3、UIProgressHUD:不量化工作,但显示工作状态或描绘进度。注意此类不在标准的SDK中

针对以上三类,分别用代码来演示应用
1\

#define INDICATOR_VIEW 999

@interface HelloController : UIViewController
{
BOOL progressShowing;
}
@end

@implementation HelloController
- (void) performAction
{
UIActivityIndicatorView
*activityIndicator = (UIActivityIndicatorView *)[self.view viewWithTag:INDICATOR_VIEW];
if (progressShowing) [activityIndicator stopAnimating]; else [activityIndicator startAnimating];
progressShowing
= !progressShowing;
}

- (id) init
{
if (self = [super init]) self.title = @"Hello World";
return self;
}

- (void)loadView
{
UIView
*contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view
= contentView;
contentView.backgroundColor
= [UIColor whiteColor];
[contentView release];

self.navigationItem.rightBarButtonItem
= [[[UIBarButtonItem alloc]
initWithTitle:
@"Do It"
style:UIBarButtonItemStylePlain
target:self
action:@selector(performAction)] autorelease];

// Add the progress indicator but do not start it
progressShowing = NO;
UIActivityIndicatorView
*activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityIndicator setCenter:CGPointMake(
160.0f, 208.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.tag
= INDICATOR_VIEW;
[contentView addSubview:activityIndicator];
[activityIndicator release];
}
@end


代码中个人觉得关键的地方在:progressShowing和activityIndicator.tag = INDICATOR_VIEW;[contentView addSubview:activityIndicator];


2\

扩展UIActionSheet函数
@interface UIActionSheet (extended)
- (void) setNumberOfRows: (NSInteger) rows;
- (void) setMessage: (NSString *)message;
@end


//Timer事件处理
- (void) incrementBar: (id) timer
{
amountDone
+= 1.0f;
UIProgressView
*progbar = (UIProgressView *)[self.baseSheet viewWithTag:PROGRESS_BAR];
[progbar setProgress: (amountDone
/ 20.0)];
if (amountDone > 20.0)
{
[self.baseSheet dismissWithClickedButtonIndex:
0 animated:YES];
[timer invalidate];
}
}

//呈现视图
- (void) presentSheet
{

if (!self.baseSheet) {
baseSheet
= [[UIActionSheet alloc]
initWithTitle:
@"Please Wait"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle: nil
otherButtonTitles: nil];
[baseSheet setNumberOfRows:
5];
[baseSheet setMessage:
@"Updating Internal Databases"];

UIProgressView
*progbar = [[UIProgressView alloc] initWithFrame:CGRectMake(50.0f, 70.0f, 220.0f, 90.0f)];
progbar.tag
= PROGRESS_BAR;
[progbar setProgressViewStyle: UIProgressViewStyleDefault];
[baseSheet addSubview:progbar];
[progbar release];
}

UIProgressView
*progbar = (UIProgressView *)[self.view viewWithTag:PROGRESS_BAR];
[progbar setProgress:(amountDone
= 0.0f)];
[NSTimer scheduledTimerWithTimeInterval:
0.5 target: self selector: @selector(incrementBar:) userInfo: nil repeats: YES];
[baseSheet showInView:self.view];
}


注意释放函数
- (void) dealloc
{
//释放baseSheet
if (self.baseSheet) [self.baseSheet release];
[super dealloc];
}


此例Timer来定时更新进度条状态
为了显示进度状态,特用了UIActionSheet来处理

在XCode4上执行,看不到应有的效果~~杯具中,看后续怎么处理

另外,
        [baseSheet setNumberOfRows:5];
        [baseSheet setMessage:@"Updating Internal Databases"];
没有执行应有的效果~~~

3\
声明已存在的类

- (void) dealloc
{
//释放baseSheet
if (self.baseSheet) [self.baseSheet release];
[super dealloc];
}


控制器中部分代码

- (void) killHUD: (id) aHUD
{
[aHUD show:NO];
[aHUD release];
}
- (void) presentSheet
{
id HUD = [[UIProgressHUD alloc] initWithWindow:[self.view superview]];
[HUD setText:
@"Downloading File. Please wait."];
[HUD show:YES];

[self performSelector:@selector(killHUD:) withObject:HUD afterDelay:
5.0];
}


类学习:

UIActivityIndicatorView类

The UIActivityIndicatorView class creates and manages an indicator showing the indeterminate progress of a task.

UIActivityIndicatorView类创建并管理指示器,此指示器用于显示当前任务的进度。

方法:
    – initWithActivityIndicatorStyle:
    – startAnimating
    – stopAnimating
    – isAnimating
   
属性:
      hidesWhenStopped表示当动画结束后此视图是否隐藏

      activityIndicatorViewStyle指示器视图样式

UIProgressView类

方法:
    – initWithProgressViewStyle:

属性:
    progress:进度,float,0.0-1.0
    progressViewStyle:进度视图样式
   
未公开:
UIProgressHUD类   

UIActionSheet的部分函数:
- (void) setNumberOfRows: (NSInteger) rows;
- (void) setMessage: (NSString *)message;

posted @ 2011-05-26 16:03  西就东城  阅读(1581)  评论(0编辑  收藏  举报