定时器显示/隐藏子视图

时间:2013-12-31 20:43:31

标签: ios hide show subview

我陷入困境,希望你们能帮忙

我有一个滚动视图,当滚动子视图的用户从下到上显示动画时。计时器然后开始计数5秒,然后调用另一种方法隐藏子视图

我实施了它按照需要工作,除了: 当子视图出现时,它几乎要隐藏,如果我滚动那一刻,子视图会静态显示并且永远不会隐藏。尝试再次滚动另一个子视图动态地处理静态的子视图(因为它是重复的或其他的)

这是我控制子视图显示和隐藏的代码

    - (void)scrollViewDidScroll:(UIScrollView *)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 selector:@selector(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;

 }

我希望能够清楚地了解并能够帮助我找出问题所在 提前谢谢

1 个答案:

答案 0 :(得分:1)

如果您按照自己的方式创建计时器,则滚动滚动视图时计时器不会触发。相反,请按以下方式创建。

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

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:使用defaultRunLoopMode而不是NSRunLoopCommonModes将计时器添加到运行循环中,NSRunLoopCommonModes是用户滚动时要让计时器触发的那个。

相关问题