IOS端的tabbar组件的pageScrolled接口没有回调positionOffset和positionOffsetPixels参数,无法实现如tab联动效果,特分享以下方案,改写tabbar组件中的scrollViewDidScroll方法即可。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == _bodyView || scrollView == _tabView) {
CGFloat x = scrollView.contentOffset.x;
CGFloat w = scrollView.contentSize.width;
CGFloat offset = 0;
CGFloat offsetPixels = 0;
CGFloat screenW = w / _tabPages.count;
NSInteger position = 0;
if(_selectedIndex > 0) {
if(x >= (screenW * _selectedIndex)) {
position = _selectedIndex;
} else {
position = _selectedIndex - 1;
}
if(x > (screenW * _selectedIndex)) {
offsetPixels = x - (screenW * _selectedIndex);
} else {
offsetPixels = x - (screenW * (_selectedIndex - 1));
}
offset = (x - (screenW * _selectedIndex)) / screenW;
} else {
offsetPixels = x;
offset = x / screenW;
}
if(offset < 0) {
offset = 1 + offset;
}
CGFloat scale = [[UIScreen mainScreen] scale];
offsetPixels = offsetPixels * scale;
NSDictionary *data = @{@"position":@(position), @"positionOffset": @(offset), @"positionOffsetPixels":@(offsetPixels)};
[self fireEvent:@"pageScrolled" params:data];
}
}