Showing posts with label UIActivityIndicatorView. Show all posts
Showing posts with label UIActivityIndicatorView. Show all posts

Thursday, January 19, 2012

When app start up, use splashView with ActivityIndicatorView

Add SplashScreenVC class in the project.
Use the below code to replace the original code in yourAppDelegate.m:

- (void)applicationDidFinishLaunching:(UIApplication *)application

{
    SplashScreenVC *splashScreenVC = [[SplashScreenVC alloc] initWithNibName:@"SplashScreenVC" bundle:nil];
    self.window.rootViewController = splashScreenVC;
    [self.window makeKeyAndVisible];
}


In SplashScreenVC, import yourAppDelegate.h and add the below code:


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self performSelector:@selector(gotoRootVC) withObject:nil afterDelay:1.0];
}

-(void)gotoRootVC
{
    
    SQLiteTutorialAppDelegate *delegate = (SQLiteTutorialAppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.window.rootViewController = delegate.tabBarController;
    [delegate.window makeKeyAndVisible];
}

-(void)viewDidLoad
{

    CGRect r = [UIScreen mainScreen].applicationFrame;
    UIView *activityView = [[[UIView alloc] initWithFrame:r] autorelease];
    self.view = activityView;
    
    activityView.backgroundColor = [UIColor blackColor];
    activityView.alpha = 0.5;
    
    UIImageView *imgView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage.png"]] autorelease];
    [activityView addSubview:imgView];
    
    CGRect wheelR = CGRectMake(r.size.width / 2 - 12, r.size.height / 2 - 12, 24, 24);
    UIActivityIndicatorView *activityWheel = [[UIActivityIndicatorView alloc] initWithFrame:wheelR];
    activityWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    activityWheel.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                      UIViewAutoresizingFlexibleRightMargin |
                                      UIViewAutoresizingFlexibleTopMargin |
                                      UIViewAutoresizingFlexibleBottomMargin);
    [activityWheel startAnimating];
    [activityView addSubview:activityWheel];
}


Tuesday, January 17, 2012

UIActivityIndicatorView

網路傳輸時 UIActivityIndicatorView 的動作

這已經是習慣了,大家都習慣要有這個符號出現。
當你的程式有使用到網路傳輸,通常就需要放一個 UIActivityIndicatorView 在畫面上,讓使用者知道目前正在傳輸中,以免讓人覺得沒有回應,像是當機一樣。

可是,為何你照著書本範例加到自己的程式卻不會動呢?
原因是:UIKit 的 main thread 一次只有做一件事,如果你在 ViewController 傳輸檔案,那麼其他的動作會等傳輸完之後再接下去做,所以你要另外再生出一個 thread 去處理 UIActivityIndicatorView 的動作。

首先,在 .h 檔的 @interface 內加上
UIActivityIndicatorView *activityIndicator;
以及
@property (nonatomicretain) UIActivityIndicatorView *activityIndicator;

另外,再加上兩個 method
-(void)actIndicatorBegin;
-(void)actIndicatorEnd;

開啓 .m 檔,在 @implementation 的下面加上
@synthesize activityIndicator;
在 (void)loadView 內把你的 UIActivityIndicatorView 加上去你要的 View 上面,我這裡加在 UIToolbar 工具列裡面的一個 UIBarButtonItem 按鈕
activityIndicator = [[UIActivityIndicatorView allocinitWithFrame:CGRectMake(002424)];
[activityIndicator setCenter:CGPointMake(1212)];
[activityIndicator setHidesWhenStopped:YES];
[activityIndicator setActivityIndicatorViewStyleUIActivityIndicatorViewStyleWhite];
[activityIndicator startAnimating];
UIBarButtonItem *activityItem = [[[UIBarButtonItem allocinitWithCustomViewactivityIndicator]autorelease];

再把這個 UIBarButtonItem 加到 UIToolbar 內
NSArray *items = [NSArray arrayWithObjects:activityItem, nil];
toolbarTop.items = items;

在處理網路傳輸之前先執行這個 thread
[NSThread detachNewThreadSelector@selector(actIndicatorBegintoTarget:self withObject:nil];

//需要時間下載檔案的程式寫在這裡
url = [NSURL URLWithString:@"http://網址/圖片檔.jpg"];
img =[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

網路傳輸完之後再執行這個 thread
[NSThread detachNewThreadSelector@selector(actIndicatorEndtoTarget:self withObject:nil];

再加上這兩個 method 開關旋轉狀態
- (void) actIndicatorBegin {
[activityIndicator startAnimating];
}
-(void) actIndicatorEnd {
[activityIndicator stopAnimating];
}

Source: http://ipdevelop.blogspot.com/2010/10/uiactivityindicatorview.html