我希望底部的工具栏在滚动视图停止滚动时消失,这是没有问题的:
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
self.toolbar.hidden = NO;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.toolbar.hidden = NO;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
self.toolbar.hidden = NO;
}
因此,此方法效果很好,但是此方法的问题在于,底部条的外观会像发疯一样闪烁。我不想要那个,所以我要让它睡一秒钟再出现。问题是
self.toolbar.hidden
在后台线程中没有响应:-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^{
NSLog(@"Hello?");
sleep(1);
self.toolbar.hidden = NO;
});
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^{
NSLog(@"Hello?");
sleep(1);
self.toolbar.hidden = NO;
});
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^{
NSLog(@"Hello?");
sleep(1);
self.toolbar.hidden = NO;
});
}
因此,在这种情况下,
self.toolbar.hidden = NO;
不执行任何操作。 NSLogs没问题,所以我认为我会尝试这种方法,但是它也不起作用:-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^{
NSLog(@"Hello?");
sleep(1);
[self unhideToolbar];
});
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^{
NSLog(@"Hello?");
sleep(1);
[self unhideToolbar];
});
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^{
NSLog(@"Hello?");
sleep(1);
[self unhideToolbar];
});
}
-(void)unhideToolbar
{
NSLog(@"toolbar unhidden");
self.toolbar.hidden = NO;
}
我无法弄清楚我在做什么错...所有的NSLogs都可以正常工作,但是取消隐藏工具栏与Async函数无关。
有人知道为什么会这样吗?
最佳答案
UI更改必须在主队列中。尝试像这样分派它:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^{
NSLog(@"Hello?");
sleep(1);
dispatch_async(dispatch_get_main_queue(), ^{
[self unhideToolbar];
});
});
关于ios - 取消隐藏工具栏在后台队列上不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24520527/