Reputation: 1770
i want hide and start the activity indicator every time the user tap on top of the mkannotationPinView.
this is my code:
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if (!self.activityIndicator) {
NSLog(@"error");
}
self.activityIndicator.hidden = NO;
[self.mapView addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
if ([view.leftCalloutAccessoryView isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = (UIImageView *)(view.leftCalloutAccessoryView);
if ([view.annotation respondsToSelector:@selector(thumbnail)]) {
imageView.image = [view.annotation performSelector:@selector(thumbnail)];
}
}
[self.activityIndicator stopAnimating];
}
i have try also to switch [self.mapView addSubview:self.activityIndicator];
with [self.view addSubview:self.activityIndicator];
but the effect is the same, notting appare on the view. whats the problem???
thanks
Upvotes: 0
Views: 4775
Reputation: 971
Queue code after StartAnimating on Main thread again. You can use
All three should work just fine.
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if (!self.activityIndicator) {
NSLog(@"error");
}
self.activityIndicator.hidden = NO;
[self.mapView addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
dispatch_async(dispatch_get_main_queue(), ^(void) {
if ([view.leftCalloutAccessoryView isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = (UIImageView *)(view.leftCalloutAccessoryView);
if ([view.annotation respondsToSelector:@selector(thumbnail)]) {
imageView.image = [view.annotation performSelector:@selector(thumbnail)];
}
[self.activityIndicator stopAnimating];
}
});
}
Explanation: If you do to UI operation without queuing up the operations in main thread effect wont be visible because UI wont show any activity until and unless you finish running the thread and move to next operating in main thread. Same is valid for addSubview removeSubview too.
Upvotes: 2
Reputation: 848
Upvotes: 0
Reputation: 2179
I don't know if this can solve your problem but I think you're not implementing the activity indicator properly. The activity indicator is used to be shown while things are being downloaded/updated/whatever in background. So.. Your code has to be something like this:
-(void)methodThatShowsTheActivityIndicator{
self.activityIndicator.hidden = NO;
[self.activityIndicator startAnimating];
[self performSelectorInBackground:@selector(backgroundProcess) withObject:self];
}
-(void)backgroundProcess{
//some process
[self performSelectorOnMainThread:@selector(finishLoad) withObject:self waitUntilDone:false];
}
-(void)finishLoad{
self.activityIndicator.hidden = YES;
[self.activityIndicator stopAnimating];
}
Hope it helped!
Upvotes: 1