使用UIActivityIndicatorView

来源:互联网 发布:佛山市2015年经济数据 编辑:程序博客网 时间:2024/05/17 19:18
UIActivityIndicatorView实例提供轻型视图,这些视图显示一个标准的旋转进度轮。当使用这些视图时,最重要的一个关键词是小。20×20像素是大多数指示器样式获得最清楚显示效果的大小。只要稍大一点,指示器都会变得模糊。图4-7显示了一个40像素的版本。
你需要在屏幕上将该指示器居中。将其放置在最方便操作的位置。作为背面清晰的视图,指示器将混合位于其后的背景视图。该背景的主要颜色帮助选择要使用的指示器样式。
对于一般用途,只需将活动指示器作为子视图添加到你要覆盖的窗口、视图、工具栏或导航栏。分配该指示器并使用一个框来初始化它,最好位于当前所使用的父视图中央。
通过将指示器的animating属性更新为YES来启动它。若要停止,将该属性设置为NO。Cocoa Touch会负责完成其余工作,在视图不使用时隐藏视图。
iPhone提供了几种不同样式的UIActivityIndicatorView类。UIActivityIndicator- ViewStyleWhite和UIActivityIndicatorViewStyleGray是最简洁的。黑色背景下最适合白色版本的外观,白色背景最适合灰色外观(如图4-7所示)。它非常瘦小,而且采用夏普风格。选择白色还是灰色时要格外注意。全白显示在白色背景下将不能显示任何内容。而UIActivityIndicatorViewStyleWhiteLarge只能用于深色背景。它提供最大、最清晰的指示器。秘诀4-6显示了创建这些简单的UIActivityIndicatorView实例的代码。

 图4-7 UIActivityIndicatorView类提
供了一个简单的旋转轮,
这意味着将以相对较小的尺寸显示

向程序中添加UIActivityIndicatorView


用法一:只显示不停旋转的进度滚轮指示器。
//显示进度滚轮指示器
-(void)showWaiting {
 
progressInd=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:
 UIActivityIndicatorViewStyleWhiteLarge];
progressInd.center=CGPointMake(self.view.center.x,240);
[self.navigationController.view addSubview:progressInd];
[progressInd startAnimating];
}
//消除滚动轮指示器
-(void)hideWaiting 
{
[progressInd stopAnimating];
}






用法二:带有半透明背景的进度轮指示器。


 
//显示进度滚轮指示器
-(void)showWaiting:(UIView*)parent {
 
    int width = 32, height = 32;
    
   CGRect frame= CGRectMake(100,200, 110,70) ;//[parent frame]; //[[UIScreenmainScreen] applicationFrame];
    int x = frame.size.width;
    int y = frame.size.height;
    
    frame = CGRectMake((x - width) / 2, (y - height) / 2, width, height);
   UIActivityIndicatorView* progressInd =[[UIActivityIndicatorView alloc]initWithFrame:frame];
   [progressIndstartAnimating];
   progressInd.activityIndicatorViewStyle =UIActivityIndicatorViewStyleWhiteLarge;
    
frame = CGRectMake((x -70)/2, (y - height) / 2 + height, 8020);
UILabel *waitingLable =[[UILabel allocinitWithFrame:frame];
waitingLable.text =@"Loading...";
waitingLable.textColor =[UIColor whiteColor];
waitingLable.font =[UIFont systemFontOfSize:15];
waitingLable.backgroundColor= [UIColor clearColor];
 
    frame = CGRectMake(10020011070) ;//[parentframe];
    UIView *theView = [[UIView allocinitWithFrame:frame];
   theView.backgroundColor =[UIColor blackColor];
    theView.alpha = 0.7;
    
    [theView addSubview:progressInd];
[theView addSubview:waitingLable];
    
   [progressInd release];
[waitingLable release];
    
    [theView setTag:9999];
    [parent addSubview:theView];
    [theView release];
}


//消除滚动轮指示器
-(void)hideWaiting 
{
   [[self.viewviewWithTag:9999]removeFromSuperview];
}