在仿微博主頁時,對其做了小小的改動善炫,效果怎樣勒叠,看大家項目的需求了:
分段控制器用的是YUSegment;
頭部下拉放大使用的HFStretchableTableHeaderView职抡,這個雖然很久了葬燎,但確實很實用;
由于是demo缚甩,所以使用的是自帶的UIPageViewController谱净,對于這個控件的一些缺陷,大家勿噴擅威,根據自己需求更換即可壕探;
在我的demo中比較重要的一個方法:
///允許同時識別多個手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
由于我的主控制器使用的是UItableView,在TableView的一個cell里放了三個子控制器郊丛,來實現(xiàn)的李请,實現(xiàn)這個效果有很多種方法瞧筛,我這個其一,三個子控制器都是繼承一個BaseTableViewController导盅,主要是方便用來判斷該控制器是否可以滑動:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//下拉放大 必須實現(xiàn)
[_stretchableTableHeaderView scrollViewDidScroll:scrollView];
//計算導航欄的透明度
UIImage *image = [UIImage imageNamed:@"pc_bg"];
CGFloat minAlphaOffset = 0;
CGFloat maxAlphaOffset = image.size.height-64;
CGFloat offset = scrollView.contentOffset.y;
CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset);
_barImageView.alpha = alpha;
//根據導航欄透明度設置title
if (alpha > 0.5) {
self.title = @"name";
} else {
self.title = @"";
}
//子控制器和主控制器之間的滑動狀態(tài)切換
CGFloat tabOffsetY = [_tableView rectForSection:0].origin.y-64;
if (scrollView.contentOffset.y >= tabOffsetY) {
scrollView.contentOffset = CGPointMake(0, tabOffsetY);
if (_canScroll) {
//通知子控制器可以滑動了较幌,其實這個通知是可以省掉了
[[NSNotificationCenter defaultCenter] postNotificationName:@"kScrollToTopNtf" object:@1];
_canScroll = NO;
//在cell中告訴子控制器可以滑動了
self.contentCell.canScroll = YES;
}
} else {
if (!_canScroll) {
scrollView.contentOffset = CGPointMake(0, tabOffsetY);
}
}
}
另外解決pageView左右滑動時,會帶動主控制器上下滑動白翻,我的實現(xiàn)原理是監(jiān)聽pageView中的手勢:
///pageView
- (void)customPageView {
NSDictionary *option = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey];
self.pageViewCtrl = [[UIPageViewController alloc]
initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:option];
self.pageViewCtrl.dataSource = self;
self.pageViewCtrl.delegate = self;
PersonalLeftViewController *ctrl1 = [[PersonalLeftViewController alloc] init];
PersonalMiddleViewController *ctrl2 = [[PersonalMiddleViewController alloc] init];
PersonalRightViewController *ctrl3 = [[PersonalRightViewController alloc] init];
self.dataArray = @[ctrl1,ctrl2,ctrl3].mutableCopy;
[self.pageViewCtrl setViewControllers:@[self.dataArray[0]]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
[self.contentView addSubview:self.pageViewCtrl.view];
for (UIView *view in self.pageViewCtrl.view.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
//監(jiān)聽拖動手勢
[view addObserver:self
forKeyPath:@"panGestureRecognizer.state"
options:NSKeyValueObservingOptionNew
context:nil];
}
}
[self.pageViewCtrl.view mas_makeConstraints:^(MASConstraintMaker* make) {
make.edges.equalTo(self.contentView);
}];
}
//監(jiān)聽拖拽手勢的回調
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
if (((UIScrollView *)object).panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
NSLog(@"bottomSView 滑動了乍炉,讓主控制器不允許滑動");
[[NSNotificationCenter defaultCenter] postNotificationName:@"PageViewGestureState" object:@"changed"];
} else if (((UIScrollView *)object).panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"結束拖拽");
[[NSNotificationCenter defaultCenter] postNotificationName:@"PageViewGestureState" object:@"ended"];
}
}
重要的部分就這么多,其實很簡單滤馍,只是我寫博客的時間不長岛琼,寫的有點亂,還是接上代碼demo吧:
demo:https://github.com/hkjin/PersonalHomePageDemo
效果圖
Untitled.gif
下面接一個朋友對于導航欄漸變做的方案纪蜒,當然啦衷恭,對于導航漸變大家應該有很多的解決方案,我個人覺得這位朋友的方案不錯纯续,貼出來讓大家參考随珠,感謝@李乾坤David
下面貼出他對導航處理的代碼:@Liqiankun
在后續(xù)JOIN項目中,重新寫了一個類(http://www.reibang.com/p/8e6dfb547061)猬错,這個類比這個方法要好很多窗看,也簡單很多。倦炒。显沈。