【问题标题】:UILabel to appear for 3 seconds after UIButton is pressed按下 UIButton 后,UILabel 会出现 3 秒
【发布时间】:2014-03-23 23:15:13
【问题描述】:

点击暂停按钮后,我希望在用户点击屏幕之前显示一个标签。我该怎么做?

到目前为止我有这个

- (IBAction)ButtonPausePressed:(id)sender {

 PauseLabel.hidden = false (//how do i make it only visible until user taps?//)

if (GameEnd != true){
    if ([GameUpdate isValid]){
        [GameUpdate invalidate];
        [BirdUpdate invalidate];
    }else{
        BirdUpdate = [NSTimer scheduledTimerWithTimeInterval:0.015
                                                      target:self
                                                    selector:@selector(UpdateBird)
                                                    userInfo:nil
                                                     repeats:YES];
        GameUpdate = [NSTimer scheduledTimerWithTimeInterval:0.025
                                                      target:self
                                                    selector:@selector(GameUpdate)
                                                    userInfo:nil
                                                     repeats:YES];
    }
}
}

【问题讨论】:

  • 使用NSTimer来做

标签: objective-c cocoa-touch uilabel nstimer


【解决方案1】:
- (IBAction)ButtonPausePressed:(id)sender { 
   PauseLabel.hidden = false
   [self performSelector:@selector(hiddenLabel) withObject:nil afterDelay:3];
   ...
}

- (void)hiddenLabel{
    PauseLabel.hidden = YES;
}

【讨论】:

    【解决方案2】:

    尝试关注

    [your_view addSubview:your_label];
    your_label.hidden = YES;
    [your_label performSelector:@selector(setHidden:) withObject:@NO afterDelay:3];
    

    【讨论】:

    • @Avt,谢谢,这对我有用。我什至不知道您使用的 performSelector 方法。
    【解决方案3】:

    试试这样:

    -(void)viewDidLoad
    {
            UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnSingleClicked:)];
    
            [singleTapGesture setDelegate:self];
             singleTapGesture.numberOfTapsRequired = 1;
            [self.view addGestureRecognizer:singleTapGesture];
    }
    
    -(void)btnSingleClicked:(UITapGestureRecognizer *)recognizer
    {
        if(recognizer.state == UIGestureRecognizerStateEnded && !self.PauseLabel.hidden)
        {
             [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
             [UIView animateWithDuration:3.0f       // 3 sec to hide it
                              delay:0.0f
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             [self.PauseLabel setAlpha:0.0f];
                                     }
                         completion:^(BOOL finished)
                                   {
                                       // remove from super view if needed.
                                       // Now you can handle touch events etc
                                       [[UIApplication sharedApplication] endIgnoringInteractionEvents];
                                   }];
        }
    }
    

    这句话背后的想法是在视图控制器的视图上添加单击手势,如果显示暂停标签并且在视图上发生触摸,请检查其状态是否结束并使用动画隐藏标签。

    【讨论】:

      【解决方案4】:

      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ PauseLabel.hidden = YES; });

      【讨论】:

        猜你喜欢
        • 2011-06-16
        • 1970-01-01
        • 1970-01-01
        • 2021-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多