实现左右滑动切换及点击切换
自用
@interface TZLSegmentController ()<UIScrollViewDelegate>
@property (nonatomic) UISegmentedControl *segmentControl;
@property (nonatomic) TZLLiveController *liveVC;
@property (nonatomic) TZLPicShowController *picShowVC;
@property (nonatomic) UIScrollView *scrollView;
@end
- (void)viewDidLoad {
[super viewDidLoad];
[self scrollView];
self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"图赏", @"直播"]];
_segmentControl.selectedSegmentIndex = 0;
_segmentControl.frame = CGRectMake(0, 0, 120, 28);
_segmentControl.tintColor = [UIColor whiteColor];
self.navigationItem.titleView = _segmentControl;
[_segmentControl bk_addEventHandler:^(id sender) {
self.scrollView.contentOffset = CGPointMake(self.segmentControl.selectedSegmentIndex * self.view.width, 0);
} forControlEvents:UIControlEventValueChanged];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGFloat offset = scrollView.contentOffset.x;
self.segmentControl.selectedSegmentIndex = offset / self.view.width;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (TZLLiveController *)liveVC {
if(_liveVC == nil) {
_liveVC = [[TZLLiveController alloc] init];
_liveVC.view.frame = CGRectMake(self.view.width, 0, self.view.width, self.view.height);
}
return _liveVC;
}
- (TZLPicShowController *)picShowVC {
if(_picShowVC == nil) {
_picShowVC = [[TZLPicShowController alloc] init];
_picShowVC.view.frame = CGRectMake(0, 0, self.view.width, self.view.height);
}
return _picShowVC;
}
- (UIScrollView *)scrollView {
if(_scrollView == nil) {
//注意: 千万不要用Monsery来布局,否则在scrollview中无法再刷新
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
[self.view addSubview:_scrollView];
_scrollView.delegate = self;
_scrollView.bounces = NO;
_scrollView.pagingEnabled = YES;
_scrollView.directionalLockEnabled = YES;
_scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
_scrollView.contentSize = CGSizeMake(2 * self.view.width, self.view.height);
_scrollView.showsHorizontalScrollIndicator = NO;
[_scrollView addSubview:self.picShowVC.view];
[_scrollView addSubview:self.liveVC.view];
}
return _scrollView;
}