Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityIndicatorView not spinning

I have this problem where I am adding an UIActivityIndicatorView to a UIScrollView; Everything is working fine, except it does not start spinning unless the UIScrollView is scrolled.

Can anyone help me with this issue?

Thank you.

here is some code:

    UIActivityIndicatorView *loaderActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    loaderActivity.center = CGPointMake(87/2,y+56/2);
    loaderActivity.tag=tag;
    [mainScrollView addSubview:loaderActivity];
    [loaderActivity startAnimating];
    [loaderActivity release];
like image 860
astazed Avatar asked Apr 03 '12 10:04

astazed


3 Answers

You need to call startAnimating on the activity indicator to have it animate. Alternatively in interface builder you can tick the "animating" tickbox.

like image 93
Benjie Avatar answered Nov 09 '22 17:11

Benjie


The fact that it's not animating until you scroll in the scroll view is a symptom that your call to startAnimating is happening in a background thread. UIKit calls should be made in the main thread.

You can verify that it's happening in a background thread by adding code like this:

if ([NSThread isMainThread]) {
    NSLog(@"Running on main thread.");
} else {
    NSLog(@"Running on background thread.");
}

You'll want to make sure all of the code you showed in your question is running on the main thread. To do this, you can change your code to look something like this:

// this code would be wherever your existing code was
[self performSelectorOnMainThread:@selector(addActivityIndicatorToView:) withObject:mainScrollView waitUntilDone:YES];

// this would be a new method in the same class that your existing code is in
- (void) addActivityIndicatorToView:(UIView*) view {
    UIActivityIndicatorView *loaderActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    loaderActivity.center = CGPointMake(87/2,y+56/2);
    loaderActivity.tag=tag;
    [view addSubview:loaderActivity];
    [loaderActivity startAnimating];
    [loaderActivity release];
}
like image 44
Greg Avatar answered Nov 09 '22 18:11

Greg


activityIndicator = [[UIActivityIndicatorView alloc] 
initWithFrame:CGRectMake(87/2,y+56/2);
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.tag=tag;
[mainScrollView addSubview:loaderActivity];
[activityIndicator startAnimating];
[activityIndicator release];
like image 34
Vinay Devdikar Avatar answered Nov 09 '22 19:11

Vinay Devdikar