UISegmentedControl结合UIScrollView,实现点击及侧滑切换控制器视图,使用addChildViewController方法以及addSubview方法,将视图添加到UIScrollView上,发现每个控制器内部的viewWillAppear方法不会走,这是因为没有把controller和它控制的view remove掉 。可以根据UIScrollView滑动时的偏移量进行手动调用viewWillAppear等方法,实现自己想要的效果。
#pragma mark - **************** UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
kLog(@"%f",scrollView.contentOffset.x);
self.segmentedControl.selectedSegmentIndex = (NSInteger)(scrollView.contentOffset.x / kScreenWidth);
if (self.segmentedControl.selectedSegmentIndex == 0) {
[_OptionalViewController viewWillAppear:YES];
}else if (self.segmentedControl.selectedSegmentIndex == 1) {
[_CurrencyViewController viewWillAppear:YES];
}else if (self.segmentedControl.selectedSegmentIndex == 2) {
[_CoinHomeViewController viewWillAppear:YES];
}
}