【问题标题】:Best way to show a UIView for a duration and hide it for an interval在一段时间内显示 UIView 并将其隐藏一段时间的最佳方法
【发布时间】:2014-07-16 06:38:07
【问题描述】:

我正在 xcode 中制作应用程序。我需要在 UIView 中显示广告一段时间,然后将其隐藏。例如。我需要将 UIView 显示 15 秒,然后将其隐藏 30 秒。做这个的最好方式是什么? 2 个 NSTimers 能完成这项工作吗?请帮忙。

【问题讨论】:

  • 是的,nstimer 是一个不错的选择。你试过了吗?

标签: objective-c nstimer nstimeinterval


【解决方案1】:
ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self performSelector:@selector(POPUpshow:) withObject:self afterDelay:0.5];
    }

-(void)POPUpshow:(id)sender
{
    PopUpview=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
    [PopUpview setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:PopUpview];
    PopUpview.hidden=NO;

    [self performSelector:@selector(popupHide) withObject:self afterDelay:1];

}

-(void)popupHide{
    PopUpview.hidden=YES;
}

希望对你有帮助

【讨论】:

    【解决方案2】:

    Swift 3 版本的accepted answer

    override func viewDidLoad() {
        super.viewDidLoad()
        self.performSelector(#selector(self.POPUpshow), withObject: self, afterDelay: 0.5)
    }
    
    func popUpshow(_ sender: Any) {
        PopUpview = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(50), width: CGFloat(320), height: CGFloat(430)))
        PopUpview.backgroundColor = UIColor.yellow
        self.view.addSubview(PopUpview)
        PopUpview.isHidden = false
        self.performSelector(#selector(self.popupHide), withObject: self, afterDelay: 1)
    }
    
    func popupHide() {
        PopUpview.isHidden = true
    }
    

    【讨论】:

      【解决方案3】:

      这个方法对你有帮助。

      [NSTimer scheduledTimerWithTimeInterval:15.0f 
      target:self selector:@selector(yourMethodToShowHide:) userInfo:nil repeats:YES];
      }
      

      您可以选择是否重复播放。 设置调用 hide/show 方法的时间间隔。

      【讨论】:

        【解决方案4】:

        你也可以使用带有延迟的 UIView 动画。

        [UIView animateWithDuration:0.2 
                              delay:_cuToast.duration 
                            options:UIViewAnimationOptionCurveLinear 
                         animations:^{
        
                             [_cuToast.view setAlpha:0];
        
                         } completion:^(BOOL finished) {
        
                             [_cuToast.view removeFromSuperview];
        
         }];
        

        我已经实现了一个带有 UIView 动画的 android 的 toast 消息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多