ios - UIButton 随动画显示和消失

标签 ios animation uibutton

我想创建一个只显示 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/

相关文章:

ios - 如何使用 xcode 界面生成器向 UIButton 添加填充?

ios - 无法更改按钮 iOS 的标题

iphone - 在 Xcode 上播放音频 (mp3) 的问题

objective-c - Objective-C 语言中需要的句法理解

jquery - 拉斐尔纸缩放动画

iOS - UIImageView 轻微故障

javascript - Chrome 和 Safari 中的 JQuery 动画错误,在 Firefox 中它按预期工作

ios - 如何更改 UIButton 框架以适应 IOS 中的图像?

ios - 在 iOS 中管理对 Web API 的异步调用

ios - XCode 8.1 更新框架不起作用