一个分段视图使用的例子

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

#import "HMSegmentedControl.h"


使用方法:

@interface ARWContactsSegmentedControlViewController ()<UIScrollViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>

@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];
    
    

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值