Reputation: 351
I would like to create a button that will show for only 5 second and then disappear. User have to click the button before it disappear. So I use animationWithDuration to set alpha to 1 and then set it back to 0. Here is my code...
__block UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:@"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:@selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
showButton.alpha = 1.0;
}
completion:nil];
[UIView animateWithDuration:0.5
delay:3.0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton removeFromSuperview];
showButton = nil;
}];
-(void)showButtonClick{
NSLog(@"click")
}
But the showButtonClick can't get called. What am I do wrong?
Upvotes: 1
Views: 1040
Reputation: 14118
Try this code:
UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:@"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:@selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
[showButton setHidden:NO];
showButton.alpha = 1.0;
}
completion: {
[UIView animateWithDuration:0.5
delay:0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton setHidden:YES];
}];
}];
-(void)showButtonClick{
NSLog(@"click")
}
Upvotes: 2
Reputation: 981
[UIView animateWithDuration:5.0
animations:^{
initWithFrame:CGRectMake(100, 0, 150, 40)
//Animation code goes here
} completion:^(BOOL finished) {
//Code to run once the animation is completed goes here
[your button remove from removefromsuperview];
}];
I can't understand,why you are using animation two times.....as your button is frame is allocated as initWithFrame:CGRectMake(0, 0, 150, 40).....check the animation code above....it will animate the button from 0 to 100....after animation completed the button will be removed...
Upvotes: 0
Reputation: 109
You should create a NSTimer or something else to call the animation. Because both of your animation are being called at the same time. That is why you were not able to click the button, thus showButtonClick wasn't being call.
Upvotes: 0