其实就是在scrollView里做控制
老板说想要一种类似支付宝的导航栏过渡切换效果,花了半天功夫把代码改好了
做完.老板看后说不要这种效果,要那种在滚动时,向上滚动显示小图标,向下显示大图标效果!😂😂😂 心酸啊
其实是有状态栏的,由于用iphone5录得实时桌面,可能是软件问题,状态栏被挡住了,大家自行忽略!
既然时间已经花费了,就简要记录下上面两种效果的代码思路吧.
由于是公司项目,就不放完整代码了.
第二张图效果实现:
1.在viewDidLoad方法里navigationBar上添加上两种view
// 1.在viewDidLoad方法里navigationBar上添加上两种view
- (void)viewDidLoad {
[super viewDidLoad];
//..................其它代码
[self.navigationController.navigationBar addSubview:self.navCoverView];
[self.navigationController.navigationBar addSubview:self.functionHeaderView];
self.functionHeaderView.hidden = NO;
}
2.设置一个属性_previousOffsetY用来记录每个上一次scrollView滚动的在Y轴的偏移量,在scrollViewWillBeginDragging:(UIScrollView *)scrollView方法里实时更新;在- (void)scrollViewDidScroll:(UIScrollView *)scrollView判断滚动趋势
#pragma mark - UIscroll代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 1 - 计算偏移量
CGFloat contentOffsetY = scrollView.contentOffset.y;
CGFloat deltaY = contentOffsetY - _previousOffsetY;
// NSLog(@"之前偏移量***%f*****现在偏移量***%f***间距**%f",
// _previousOffsetY,scrollView.contentOffset.y,deltaY);
if (deltaY > 0) { // 向上滚动显示大图
self.functionHeaderView.hidden = NO;
self.navCoverView.hidden = !self.functionHeaderView.hidden;
} else { // 向下显示小图
self.functionHeaderView.hidden = YES;
self.navCoverView.hidden = !self.functionHeaderView.hidden;
}
self.previousOffsetY = contentOffsetY;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_previousOffsetY = scrollView.contentOffset.y;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
UIView *view = nil;
if (self.functionHeaderView.hidden == NO) {
view = self.functionHeaderView;
}else {
view = self.navCoverView;
}
CGFloat top = view.bounds.size.height - 10;
self.tableView.contentInset = UIEdgeInsetsMake(top, 0, 50, 0);
}
第一张图效果实现
1. 设置大图为tableHeaderView,navigationBar上添加小导航图
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.tableHeaderView = self.functionHeaderView;
[self.navigationController.navigationBar addSubview:self.navCoverView];
self.navCoverView.hidden = NO;
self.navCoverView.alpha = 0;
}
2.在scrollViewDidScroll代理方法里设置渐变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat alphaMinOffset = - 64;
CGFloat alphaMaxOffset = self.tableView.tableHeaderView.height - 64;
CGFloat scrollOffset = scrollView.contentOffset.y;
// QYLog(@"****偏移了**** %zd",offset);
// alpha 从 0 --> 1--> Max
CGFloat alpha = (scrollOffset - alphaMinOffset) / (alphaMaxOffset - alphaMinOffset);
UIImageView* barImageView = self.navigationController.navigationBar.subviews.firstObject;
barImageView.alpha = 1 - alpha;
// QYLog(@"透明度****%f",alpha);
[self.navigationController.navigationItem.titleView setTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0]];
self.navCoverView.alpha = alpha;
}
顺便记录一下,这个项目其它踩到的坑吧
1.设置tableview样式为UITableViewStyleGrouped
记得设置sectionFooterHeight 和 sectionHeaderHeight 的值,我此次项目由于navigatinBar的childView高度不断变化 ,设置 self.automaticallyAdjustsScrollViewInsets = NO;
/**
* 设置表格
*/
- (void)setupTableView {
self.tableView.allowsSelection = NO;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = GlobeTablviewBackgroundColor;
self.tableView.bounces = NO;
self.tableView.sectionFooterHeight = 5;
// 设置内边距
self.automaticallyAdjustsScrollViewInsets = NO;
CGFloat top = self.functionHeaderView.bounds.size.height - 12;
CGFloat bottom = self.functionHeaderView.bounds.size.height - 12;
self.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
// 滚动条内边距
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(top, 0, bottom, 0);
}