一个分段视图使用的例子

参考:https://github.com/HeshamMegid/HMSegmentedControl

#import "HMSegmentedControl.h"


使用方法:

@interface ARWContactsSegmentedControlViewController ()

@property (nonatomic, strong) HMSegmentedControl *topSegmentedControl;
@property (nonatomic, strong) UIScrollView *contentScrollView;

@property (nonatomic, strong) ARWRecommendView *recommendView;
@property (nonatomic, strong) ARWContactsView *contactView;
@property (nonatomic, strong) ARWFriendSearchDisplayController *recommendSearchController;
@property (nonatomic, strong) ARWFriendSearchDisplayController *contactSearchController;

@end

@implementation ARWContactsSegmentedControlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Contacts";
    
    [self configureScrollView];
    
    //推荐搜索
    self.recommendSearchController = [[ARWFriendSearchDisplayController alloc] initWithSearchBar:_recommendView.searchBar contentsController:self];
    [self.recommendSearchController setDelegate: self];
    
    
    //联系人搜索
    self.contactSearchController = [[ARWFriendSearchDisplayController alloc] initWithSearchBar:_contactView.searchBar contentsController:self];
    [self.contactSearchController setDelegate: self];
    
}




- (void)configureScrollView
{
    //上面分段选择头
    _topSegmentedControl = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, IOS_StatusBar_Height+IOS_NavigationBar_Height, IOS_ScreenWidth, IOS_NavigationBar_Height)];
    _topSegmentedControl.sectionTitles = @[@"Recommend", @"Contacts"];
    _topSegmentedControl.selectedSegmentIndex = 1;
    _topSegmentedControl.backgroundColor = COLOR_BACKGROUND_SELFRED;
    _topSegmentedControl.textColor = COLOR_BACKGROUND_BLACK;
    _topSegmentedControl.selectedTextColor = COLOR_BACKGROUND_WHITE;
    _topSegmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
    _topSegmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    _topSegmentedControl.tag = 0;
    
    
    CGFloat scrollContentHeight = IOS_ScreenHeight-IOS_StatusBar_Height-IOS_NavigationBar_Height*2-IOS_TabBar_Height;
    
    __weak typeof(self) weakSelf = self;
    [_topSegmentedControl setIndexChangeBlock:^(NSInteger index) {
        [weakSelf.contentScrollView scrollRectToVisible:CGRectMake(IOS_ScreenWidth * index, 0, IOS_ScreenWidth, scrollContentHeight) animated:YES];
    }];
    //self.navigationItem.titleView = _topSegmentedControl;
    
    [self.view addSubview:_topSegmentedControl];
    
    
    
    //水平滚动内容视图
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, IOS_StatusBar_Height+IOS_NavigationBar_Height*2, IOS_ScreenWidth,  scrollContentHeight)];
    self.contentScrollView.backgroundColor = COLOR_BACKGROUND_WHITE;
    self.contentScrollView.contentSize = CGSizeMake(IOS_ScreenWidth*2, scrollContentHeight);
    self.contentScrollView.delegate = self;
    [self.contentScrollView scrollRectToVisible:CGRectMake(0, 0, IOS_ScreenWidth, scrollContentHeight) animated:NO];
    self.contentScrollView.contentInset = UIEdgeInsetsZero;
    self.contentScrollView.alwaysBounceHorizontal = YES;
    self.contentScrollView.alwaysBounceVertical = NO;
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.userInteractionEnabled = YES;
    self.contentScrollView.bounces = NO;
    
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.bouncesZoom = NO;
    self.contentScrollView.maximumZoomScale = 1.0;//允许放大
    self.contentScrollView.minimumZoomScale = 1.0;//允许放大
    
    [self.view addSubview:self.contentScrollView];
    
    
    //推荐
    _recommendView = [[ARWRecommendView alloc] initWithFrame:CGRectMake(0, 0, IOS_ScreenWidth, scrollContentHeight)];
    [self.contentScrollView addSubview:_recommendView];
    [_recommendView.tableView.panGestureRecognizer requireGestureRecognizerToFail:self.contentScrollView.panGestureRecognizer ];
    
    //通讯录
    _contactView = [[ARWContactsView alloc] initWithFrame:CGRectMake(IOS_ScreenWidth, 0, IOS_ScreenWidth, scrollContentHeight)];
    [self.contentScrollView addSubview:_contactView];
    [_contactView.tableView.panGestureRecognizer requireGestureRecognizerToFail:self.contentScrollView.panGestureRecognizer ];

    
}


- (void)viewWillAppear:(BOOL)animated
{
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
    NSLog(@"Selected index %ld (via UIControlEventValueChanged)", (long)segmentedControl.selectedSegmentIndex);
}

- (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl {
    NSLog(@"Selected index %ld", (long)segmentedControl.selectedSegmentIndex);
}




#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (scrollView.contentOffset.y < 0) {
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
    }
    
    if (scrollView.contentOffset.y > 0) {
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
    }
    
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    
    [_topSegmentedControl setSelectedSegmentIndex:page animated:YES];
    
    

}



你可能感兴趣的:(IOS,+,Mac,OS,Objective-C)