Reputation: 5745
I'm making an app and have an IBAction that is triggered when the user hits a button. Heres the code:
- (IBAction)expand:(id)sender{
UIImage *statusImage = [UIImage imageNamed:@"status.png"];
[activityImageView setImage:statusImage];
CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 0.25f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1;
[activityImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[[NSUserDefaults standardUserDefaults]setFloat:self.conditionsImageView.bounds.size.height forKey:@"rect"];
[[NSUserDefaults standardUserDefaults]setFloat:self.conditionsImageView.bounds.origin.y forKey:@"y"];
NSURL *uuu = [NSURL URLWithString:@"http://api.wunderground.com/api/3c158b3b3cd6ce90/animatedradar/q/autoip.gif?newmaps=1&timelabel=1&smooth=1&timelabel.y=10&num=8&delay=50&width=640&height=960"];
self.conditionsImage = [UIImage animatedImageWithAnimatedGIFURL:uuu duration:8];
self.conditionsImageView.bounds = CGRectMake(0, 0, 320, self.view.bounds.size.height + 20);
self.conditionsImageView.image = conditionsImage;
exp.hidden = 0;
exp1.hidden = 1;
}
See, the ImageView activityImageView doesn't show up until the loading of the other image, from that url, is done and it is not animating. Anyone see any problems that might be causing this?
Upvotes: 0
Views: 624
Reputation: 385998
I assume you are using my implementation of +[UIImage animatedImageWithAnimatedGIFURL:duration:]
.
This method is synchronous. It doesn't return until it has loaded all of the data from the URL. So when you call it on the main thread, you're blocking the main thread, thus preventing your other animation from starting (and preventing any user interactions).
Instead of using +[UIImage animatedImageWithAnimatedGIFURL:duration:]
, use +[NSURLConnection connectionWithRequest:delegate:]
or +[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]
to load the data from the URL in the background. When it's all loaded, then use +[UIImage animatedImageWithAnimatedGIFData:duration:]
to create the image.
Upvotes: 1