UIButton显示并随动画消失

时间:2014-12-17 06:51:37

标签: 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无法调用。我做错了什么?

3 个答案:

答案 0 :(得分:2)

试试这段代码:

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

答案 1 :(得分:0)

你应该创建一个NSTimer或其他东西来调用动画。因为两个动画都是同时被调用的。这就是为什么你无法点击按钮,因此showButtonClick没有被调用。

答案 2 :(得分:0)

[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 ....动画完成后按钮将被删除...