As a newcomer to IOS programming this particular issue on my first app took a bit of working out so this is a tutorial for others struggling with the same issue.
The ultimate objective of this code is to get a out like shown below
Firstly things to note.
- UI components need to stay on the main thread, the main thread needs to perform the UI updates. (Cocoa Fundamentals Guide, page 164: “All UIKit objects should be used on the main thread only.”)
- This tutorial/code sample is about adding a UIActivityIndicatorView programmatically
- You will need #import
- in this code activityIndicator is a property
I am not entirely sure this is the best way as I have only been doing IOS for a short while but it works for me.
So I suggest
Firstly start by creating a method where you start the whole thing off (probably triggered from a button press- in this sample it is showNearby.
Secondly create a NSTimer and start it immediately calling the show activity selector
Thirdly start your marker/pin loading method using the performSelectorInBackground method.
Finally remove the indicator when you have completed your marker/pin showing / loading
That’s it you will have a activity indicator that appears whilst loading and is removed once done.
The code for this is shown here.
-(IBAction)showNearby:(id)sender { NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ShowActivityIndicator) userInfo:nil repeats:NO]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:UITrackingRunLoopMode]; [self performSelectorInBackground:@selector(loadMarkers) withObject: nil]; } -(void) ShowActivityIndicator { activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] ; CGRect frame = activityIndicator.frame; frame.origin = CGPointMake(290, 12); frame.size = CGSizeMake(100, 100); activityIndicator.backgroundColor=[UIColor blackColor]; CALayer *layer =[activityIndicator layer]; layer.cornerRadius = 8.0; activityIndicator.alpha=0.7; activityIndicator.frame = frame; activityIndicator.center = self.view.center; activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; activityIndicator.tag = 2; [self.view addSubview:activityIndicator]; [self.view bringSubviewToFront:activityIndicator]; [activityIndicator startAnimating]; } -(void) loadMarkers { //code to load markers then remove activityIndicator [activityIndicator stopAnimating]; [activityIndicator removeFromSuperview]; }
'iOS' 카테고리의 다른 글
iOS5에서 NSJSONSerialization을 사용해서 JSON 다루기 예제 (0) | 2012.08.28 |
---|---|
GCD와 Block을 이용해서 비동기 방식으로 URL 이미지 로드하기 (0) | 2012.08.27 |
UIProgressHUD / 어플 실행 및 , 데이터 호출시 "로딩중" 메세지처리 (0) | 2012.08.27 |
aes-128을 사용한 app과 server의 통신 (1) | 2012.08.26 |
PullToRefresh iOS 5 and ARC Tutorial (0) | 2012.08.22 |