OGeek|极客世界-中国程序员成长平台

标题: ios - 显示/隐藏 subview 的计时器 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:49
标题: ios - 显示/隐藏 subview 的计时器

我遇到了一些事情希望你们能帮忙

我有一个 scrollview ,当用户滚动 subview 时,会出现从下到上的动画。然后计时器开始计数 5 秒,然后调用另一个方法来隐藏 subview

我实现了,它按要求工作,除了: 当 subview 出现并且几乎要隐藏时,如果我滚动那一刻, subview 会静态显示并且从不隐藏。尝试再次滚动另一个 subview 动态地在静态 View 上工作(因为它重复或其他)

这是我控制 subview 显示和隐藏的代码

    - (void)scrollViewDidScrollUIScrollView *)scrollView
{
    if(!show){


        [self showSubview];
          if (!myidleTimer)
        [self resetIdleTimer];

    }

}


-(void)resetIdleTimer
{


    //convert the wait period into minutes rather than seconds
    int timeout = kApplicationTimeoutInMinutes;// equal to 5 seconds
    [myidleTimer invalidate];
    myidleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selectorselector(idleTimerExceeded) userInfo:nil repeats:NO];

}

-(void)idleTimerExceeded
{

    if (show){
         [myidleTimer invalidate];
        [self hideSubview];
        show=false;

    }
}

"show"是一个 bool 值,用于确保何时隐藏和何时显示 她是显示/隐藏实现

  -(void)hideSubview{


    [UIView animateWithDuration:0.5
                     animations:^{
     subview.frame = CGRectMake(0, screenWidth, screenHeight, 60);//move it out of screen
                     } completion:^(BOOL finished) {
                         [subview removeFromSuperview];
                         subview.frame=CGRectMake(0,screenWidth, screenHeight, 0);
                     }];
        show=false;
}


-(void) showSubview{

    subview = [[UIView alloc] init ];

    [self.view addSubview:subview];
    subview.frame = CGRectMake(0, screenWidth, screenHeight, 60);
    [UIView animateWithDuration:1.0
                     animations:^{
                         subview.frame = CGRectMake(0, screenWidth-60, screenHeight, 60);
                     }];

        show=TRUE;

 }

我希望它足够清楚,可以理解并能够帮助我识别问题 提前致谢



Best Answer-推荐答案


如果您按照自己的方式创建计时器,则在 ScrollView 时计时器不会触发。相反,请按如下方式创建它。

NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selectorselector(doStuff userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:使用 defaultRunLoopMode 而不是 NSRunLoopCommonModes 将计时器添加到运行循环,这是您希望在用户滚动时触发计时器的模式。

关于ios - 显示/隐藏 subview 的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20862993/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4