iPhone控件之UIActivityView

//
// UITestViewController.m
// UITest
//

#import "UITestViewController.h"

@implementation UITestViewController


- (void)viewDidLoad {

[super viewDidLoad];

[self.view setBackgroundColor:[UIColor blackColor]];

UIActivityIndicatorView *myActivityView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

CGRect activityFrame = CGRectMake(130,100,50,50);
[myActivityView setFrame:activityFrame];

[myActivityView startAnimating];

[self.view addSubview:myActivityView];

[myActivityView release];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}

@end

在UIActivityIndicatorView与UITableView混用时,如果直接在点击表格的时间中来调用UIActivityIndicatorView,可能该视图的出现仍然会延迟,解决方法是重新开启一个线程来执行该方法,实例如下:

- (void)viewDidLoad
{
 myview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)];
    [myview setBackgroundColor:[UIColor blackColor]];
    [myview setAlpha:0.5];
    myActivityView = [[UIActivityIndicatorView alloc] 
                      initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [myActivityView setFrame:CGRectMake(0, 0, 50, 50)];
    myActivityView.center = self.view.center;
    myActivityView.hidesWhenStopped = YES;
    
    UILabel *mylabel = [[UILabel alloc] initWithFrame:CGRectMake(myActivityView.frame.origin.x-50,myActivityView.frame.origin.y + 50, 200, 50)];
    mylabel.text = @"数据加载中⋯";
    mylabel.backgroundColor = [UIColor clearColor];
    mylabel.font = [UIFont systemFontOfSize:22];
    mylabel.textColor = [UIColor whiteColor];
    [myview addSubview:mylabel];
    [myview addSubview:myActivityView];
}

在点击表格中开启线程:

//处理点击事件
-(void)tableView:(UITableView*)tableView 
didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{    
    
    [NSThread detachNewThreadSelector:@selector(addActivity) toTarget:self withObject:nil]; 

//⋯⋯⋯⋯⋯⋯

}

实现方法:

- (void)addActivity{
    [self.view addSubview:myview];
    [myActivityView startAnimating];
}
原文地址:https://www.cnblogs.com/foxmin/p/2393603.html