본문 바로가기

개발도구/iOS - 아이폰 개발

[iphone] UIActivityIndicatorView spinner for long running operations

http://agilewarrior.wordpress.com/2012/04/03/how-to-display-uiactivityindicatorview-spinner-for-long-running-operations/

쓰레드 값을 주어 마치 indicatior 가 움직이게 처리하는 것이라 착각하게 만듬.

#import "rcsViewController.h"

@implementation rcsViewController
@synthesize myLabel;
@synthesize myButton;

- (IBAction)process:(id)sender {    
        
    // replace right bar button 'refresh' with spinner
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.center = CGPointMake(160, 240);
    spinner.hidesWhenStopped = YES;
    [self.view addSubview:spinner];    
    [spinner startAnimating];
        
    // how we stop refresh from freezing the main UI thread
    dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
    dispatch_async(downloadQueue, ^{

        // do our long running process here
        [NSThread sleepForTimeInterval:10];
        
        // do any UI stuff on the main UI thread
        dispatch_async(dispatch_get_main_queue(), ^{
            self.myLabel.text = @"After!";
            [spinner stopAnimating];
        });
        
    });
    dispatch_release(downloadQueue);
}

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

- (void)viewDidUnload
{
    [self setMyLabel:nil];
    [self setMyButton:nil];
    [super viewDidUnload];
}

@end