【问题标题】:UIButton show and disappear with animationUIButton 随动画显示和消失
【发布时间】:2015-02-15 15:52:36
【问题描述】:

我想创建一个仅显示 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。我做错了什么?

【问题讨论】:

  • 你什么时候点击按钮的?在按钮完全可见之后还是在第一个动画期间可见?
  • 按钮完全可见后。延迟的事情似乎不起作用。

标签: ios animation uibutton


【解决方案1】:

试试这个代码:

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")
}

【讨论】:

  • 还是不行。我找到了答案。谢谢你的回复:)
【解决方案2】:
[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];
}];

我不明白,为什么你使用动画两次.....因为你的按钮是框架被分配为 initWithFrame:CGRectMake(0, 0, 150, 40).....检查动画代码上面....它将动画按钮从0到100....动画完成后按钮将被删除...

【讨论】:

    【解决方案3】:

    您应该创建一个 NSTimer 或其他东西来调用动画。因为您的两个动画同时被调用。这就是您无法单击按钮的原因,因此未调用 showButtonClick。

    【讨论】:

    • 是的,你说得对。我添加了一个 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{});
    • 你知道为什么animateWithDuration:delay:optoins:animations:completion:方法中的delay参数还是会导致动画阻塞buttonClick吗?
    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多