我想创建一个只显示 5 秒然后消失的按钮。用户必须在按钮消失之前单击它。所以我使用 animationWithDuration 将 alpha 设置为 1,然后将其设置回 0。这是我的代码...
__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")
}
但是无法调用 showButtonClick。我做错了什么?
最佳答案
试试这个代码:
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")
}
关于ios - UIButton 随动画显示和消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519833/