• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 显示/隐藏 subview 的计时器

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:49:26 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

我有一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap