0
您好,我似乎有一些关于我的视图控制器的问题。当用户向下滚动我的标题栏时,会慢慢淡出,一旦用户向上滚动标题栏会慢慢淡入。我的问题是当向下滚动时,标题栏有时会卡住并变得不连贯,如下图所示:向下滚动:标题栏显示和隐藏
白色条带卡,无需清洁的出现和消失的效果。下面是代码:
#warning new code for hide header bar
/**
* Catch scroll event for calculating Show and hide header view
* Note that _menu (tableView) is the scrollable control
*/
[_contentTableView addObserver:self
forKeyPath:@"contentOffset"
options:NSKeyValueObservingOptionOld
context:nil];
_hidesBarsOnScroll = YES;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
UITableView *tempTableView = _contentTableView;
CGPoint oldOffset = [(NSValue *)change[NSKeyValueChangeOldKey] CGPointValue];
if (!_hidesBarsOnScroll || tempTableView.contentOffset.y == oldOffset.y)
return;
// Show on scroll up
if (_barsHidden &&
tempTableView.contentOffset.y < oldOffset.y &&
tempTableView.contentOffset.y + tempTableView.bounds.size.height < tempTableView.contentSize.height) // Skip on bottom
{
if (tempTableView.contentOffset.y < 60) {
_header.hidden = NO;
float alpha = 1.0 - tempTableView.contentOffset.y/64.0;
[_header setBackgroundColor:[UIColor colorWithRed: 0.1216 green: 0.1569 blue: 0.2078 alpha: alpha]];
}
if (tempTableView.contentOffset.y<=0)
{
CGRect contentViewFrame = _contentTableView.frame;
contentViewFrame.origin.y = 64;
_contentTableView.frame = contentViewFrame;
[_header setBackgroundColor:_headerColor];
_barsHidden = NO;
}
}
// Hide on scroll down
if (!_barsHidden &&
tempTableView.contentOffset.y > 0 && // Skip on top
tempTableView.contentOffset.y > oldOffset.y)
{
float alpha = 1.0 - tempTableView.contentOffset.y/64.0;
[_header setBackgroundColor:[UIColor colorWithRed: 0.1216 green: 0.1569 blue: 0.2078 alpha: alpha]];
CGRect contentViewFrame = _contentTableView.frame;
contentViewFrame.origin.y = 84-tempTableView.contentOffset.y;
_contentTableView.frame = contentViewFrame;
if (tempTableView.contentOffset.y >= 64) {
_barsHidden = YES;
_header.hidden = YES;
}
}
}
谢谢你的时间,非常感谢!当我能简化它时,我是否超载自己并过度使用代码? – pLokie
我这么认为。我认为我建议的代码是所有需要的。 – danh