類似于新浪微博個(gè)人主頁(yè)的UI效果在iOS中是很常見(jiàn)的, 實(shí)現(xiàn)的難度就在于既可以左右滑動(dòng), 也可以上下滑動(dòng).
通常的做法是在一個(gè)橫向滾動(dòng)的ScrollView
上添加幾個(gè)豎向的TableVIew
, 最后加一個(gè)HeaderView
在ScrollView
的superView
上, 這么做的話會(huì)有一個(gè)問(wèn)題就是HeaderView
無(wú)法豎向滑動(dòng).
有看到其它博客說(shuō)給HeaderView
加一個(gè)滑動(dòng)手勢(shì), 來(lái)模擬TableView
的滑動(dòng), 但是就缺少了ScrollView
回彈的彈簧動(dòng)畫, 再后來(lái)有自己來(lái)實(shí)現(xiàn)彈簧動(dòng)畫的思路, 大佬果然棒棒噠, 什么困難都可以克服 ??
但是懶人懶辦法, 有一個(gè)最簡(jiǎn)單有效的方法就是把TableVIew
的PanGesture
添加到最底層的superView
上就可以了, 我們只需要對(duì)他做一些控制.
- (void)_buildIndex {
NSInteger index = (NSInteger)(_mainScrollView.contentOffset.x / self.width);
index = MIN(_tableViews.count - 1, index);
index = MAX(0, index);
[self removePanGestureForIndex:_index];
_index = index;
[self addPanGestureForIndex:_index];
_currentIndex = index;
if (self.currentIndexDidChange) {
self.currentIndexDidChange(_currentIndex);
}
}
- (void)addPanGestureForIndex:(NSInteger)index {
if (index < _tableViews.count && index >= 0) {
UITableView *tb = _tableViews[index];
[self addGestureRecognizer:tb.panGestureRecognizer];
}
}
- (void)removePanGestureForIndex:(NSInteger)index {
if (index < _tableViews.count && index >= 0) {
UITableView *tb = _tableViews[index];
[self removeGestureRecognizer:tb.panGestureRecognizer];
}
}
最終實(shí)現(xiàn)效果如下:
這里還有另外一位大佬寫的一個(gè)庫(kù), 實(shí)現(xiàn)思路是相同的 HJTabViewController