Reputation: 2545
Now i am working in iPhone application, Using UIImageView to create a animation and it's working fine, then i tried to call one method for when the animation once completed then the button to show on the screen, but that button showing first then the animation starts, i want once animation completed then the button showing on the screen, how to fix this issue? please help me
Thanks in Advance
I tried this for your reference:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray * imageArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"cardopen.png"],
[UIImage imageNamed:@"middlecard.png"],
[UIImage imageNamed:@"thirdcard.png"],
[UIImage imageNamed:@"a.png"],
[UIImage imageNamed:@"cardopen1.png"],
[UIImage imageNamed:@"middlecard2.png"],
[UIImage imageNamed:@"thirdcard1.png"],
[UIImage imageNamed:@"2.png"],
nil];
UIImageView * cardsImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 400, 300)];
cardsImage.animationImages = imageArray;
cardsImage.animationDuration = 8.0;
cardsImage.animationRepeatCount=1; // one time looping only
[self.view addSubview:cardsImage];
[cardsImage startAnimating];
[self method1]; // To Call a method1 to show UIButton (Once the animation is completed, then to call a method1)
}
-(void)method1
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(10, 40, 50, 50);
[self.view addSubview:btn];
}
Upvotes: 1
Views: 491
Reputation: 31091
Use
[self performSelector:@selector(method1) withObject:nil afterDelay:8.0];
instead of [self method1];
Upvotes: 1
Reputation: 7226
This is because you are putting all of your code in ViewDidLoad
. The whole method will be processed . Plus you have your animation duration set to 0.8 which is a slow animation.So the button would appear normally and the animation would continue along with it and then even more. So what you actually have to do is use an NSTimer
or use the answers provided to you here :)
Upvotes: 0
Reputation:
(Why doesn't anybody understand the concept of asynchronous methods? Is this that hard?)
[cardsImage startAnimating];
returns immediately, not after the animation finished. You have to set an explicit timeout:
NSTimer *tm = [NSTimer scheduledTimerWithTimeInteral:imageView.animationDuration * imageView.animationCount // assuming animationCount is not 0
target:self
selector:@selector(method1)
repeats:NO
userInfo:nil];
Upvotes: 2