iOS UISegmentControl實現(xiàn)自定義分欄效果
本文實例為大家分享了iOS UISegmentControl實現(xiàn)自定義分欄效果的具體代碼,供大家參考,具體內(nèi)容如下

iOS 自帶的UISegmentControl實現(xiàn)的就是類似上圖的效果
但是很多用處 一般這兩個分欄是兩個tableView,需要左右滑動來響應分欄
下面來講述這樣的效果是怎么實現(xiàn)的呢?

主要那里有一根短紅線,需要滑動 來切換tableView
先自定義一個SegmentView
.h
//定義block,用來傳遞點擊的第幾個按鈕 typedef void (^PassValueBlock)(NSInteger index); @interface BCLCommunitySegmentView : UIView //定義一下block @property (nonatomic, strong) PassValueBlock returnBlock; @property (nonatomic, strong) UIImageView *selectImage;//這個就是短紅線 //初始化數(shù)組,傳入frame和名稱 - (id) initWithFrame:(CGRect)frame withTitleArray:(NSArray *)array; //block傳遞值方法 - (void)setReturnBlock:(PassValueBlock)returnBlock; @end
在SegmentView.m中
循環(huán)創(chuàng)建按鈕,幾個分欄創(chuàng)建幾個按鈕
- (void)creatSegmentView {
? ? //設置按鈕的寬度
? ? _itemWidth = self.frame.size.width / _itemCounts;
? ? //循環(huán)創(chuàng)建按鈕
? ? for (int i = 0; i < _itemCounts; i++) {
? ? ? ? UIButton *button ?= [[UIButton alloc]initWithFrame:CGRectMake((i + 1) *_itemWidth/2, 0, _itemWidth/2, self.frame.size.height)];
? ? ? ? [self addSubview:button];
? ? ? ??
? ? ? ? //設置button的字
? ? ? ? [button setTitle:_titleArray[i] forState:UIControlStateNormal];
? ? ? ? //設置button的字顏色
? ? ? ??
? ? ? ? [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
? ? ? ? //設置字體大小
? ? ? ? button.titleLabel.font = [UIFont systemFontOfSize:20];
? ? ? ? //設置居中顯示
? ? ? ? button.titleLabel.textAlignment = NSTextAlignmentCenter;
? ? ? ? //設置tag值
? ? ? ? button.tag = 1000 + i;
? ? ? ? //添加點擊事件
? ? ? ? [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? //如果是第一個,默認被選中
? ? ? ? if (i == 0) {
? ? ? ? ? ? button.selected = YES;
? ? ? ? }
? ? }
? ??
? ??
? ? //添加一個select
? ? _selectImage = [[UIImageView alloc]initWithFrame:CGRectMake(_itemWidth / 2, self.frame.size.height - 2, _itemWidth / 2, 2)];
? ? _selectImage.image = [UIImage imageNamed:@"bcl_bg_community_segment_color_line"];
? ? [self addSubview:_selectImage];
}然后設置按鈕的點擊事件,將點擊到哪個按鈕 回調過去
-(void)buttonAction:(UIButton *)button{
? ??
? ? //當button被點擊,所有的button都設為未選中狀態(tài)
? ? for (UIView *view in self.subviews) {
? ? ? ? if ([view isKindOfClass:[UIButton class]]) {
? ? ? ? ? ? UIButton *subButton = (UIButton*)view;
? ? ? ? ? ? subButton.selected = NO;
? ? ? ? ? ? subButton.titleLabel.font = [UIFont systemFontOfSize:20];
? ? ? ? }
? ? }
? ? //然后將選中的這個button變?yōu)檫x中狀態(tài)
? ? button.selected = YES;
? ??
? ? //通過當前的tag值設置select的位置
? ? NSInteger index = button.tag - 1000;
? ? [UIView animateWithDuration:0.3 animations:^{
? ? ? ? self->_selectImage.frame = CGRectMake((1 + index)*_itemWidth/2, _selectImage.frame.origin.y, self->_selectImage.frame.size.width, _selectImage.frame.size.height);
? ? }];
? ??
? ? _returnBlock(index);
}在需要展現(xiàn)的controller中
.h
@interface BCLCommunityView : UIView @property (nonatomic, strong) UIScrollView *scrollerView; @property(nonatomic ,strong) UITableView *circleTableView; @property(nonatomic ,strong) UITableView *squreTableView; @property (nonatomic, strong)BCLCommunitySegmentView *segmentView; @end
在.m中用scrollView實現(xiàn)分欄的兩個tableView的滑動
- (instancetype) initWithFrame:(CGRect)frame {
? ? if(self = [super initWithFrame:frame]) {
? ? ? ? [self setSegmentView];
? ? ? ??
? ? ? ? _circleTableView = [self loadTableView];
? ? ? ? _squreTableView = [self loadTableView];
? ? ? ??
? ? ? ? _circleTableView.tag = 1;
? ? ? ? _squreTableView.tag = 2;
? ? ? ??
? ? ? ? _scrollerView = [[UIScrollView alloc] init];
? ? ? ? _scrollerView.frame = CGRectMake(0, 104, KScreenW, KScreenH);
? ? ? ? _scrollerView.pagingEnabled = YES;
? ? ? ? _scrollerView.scrollEnabled = YES;
? ? ? ? _scrollerView.contentSize = CGSizeMake(KScreenW * 2, KScreenH);
? ? ? ? _scrollerView.bounces = YES;
? ? ? ? _scrollerView.delegate = self;
? ? ? ??
? ? ? ? [_scrollerView addSubview:_circleTableView];
? ? ? ? [_scrollerView addSubview:_squreTableView];
? ? ? ??
? ? ? ? _circleTableView.frame = CGRectMake(0, 0, KScreenW, KScreenH);
? ? ? ? _squreTableView.frame = CGRectMake(KScreenW, 0, KScreenW, KScreenH);
? ? ? ? [self addSubview:_scrollerView];
? ? }
? ? return self;
}
- (UITableView *)loadTableView
{
? ? UITableView ?*tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH) style:UITableViewStyleGrouped];
? ? tableView.showsVerticalScrollIndicator = NO;
? ? [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
? ??
? ? tableView.dataSource = self;
? ??
? ? [self addSubview:tableView];
? ? return tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
? ? if(tableView.tag == 1) {
? ? ? ? return 3;
? ? } else {
? ? ? ? ?return 2;
? ? }
? ?
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
? ? if(tableView.tag == 1) {
? ? ? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
? ? ? ? if(!cell) {
? ? ? ? ? ? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
? ? ? ? ? ??
? ? ? ? }
? ? ? ? cell.backgroundColor = [UIColor redColor];
? ? ? ??
? ? ? ? cell.textLabel.text = @"11111";
? ? ? ? return cell;
? ? } else {
? ? ? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
? ? ? ? if(!cell) {
? ? ? ? ? ? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
? ? ? ? }
? ? ? ? return cell;
? ? }
? ??
}scrollView代理 滑動scrollerView實現(xiàn)小紅條的滑動
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
? ? CGRect frame = _segmentView.selectImage.frame;
? ? if(scrollView.contentOffset.x / KScreenW == 0) {
? ? ? ? [UIView animateWithDuration:0.1 animations:^{
? ? ? ? _segmentView.selectImage.frame = CGRectMake(KScreenW / 4, frame.origin.y, frame.size.width, frame.size.height);
? ? ? ? }];
? ? } else if(scrollView.contentOffset.x / KScreenW == 1){
? ? ? ? [UIView animateWithDuration:0.1 animations:^{
? ? ? ? ? ? _segmentView.selectImage.frame = CGRectMake(KScreenW / 2, frame.origin.y, frame.size.width, frame.size.height);
? ? ? ? }];
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
iOS實現(xiàn)實時檢測網(wǎng)絡狀態(tài)的示例代碼
網(wǎng)絡連接狀態(tài)檢測對于我們的iOS開發(fā)來說是一個非常通用的需求。下面這篇文章主要就給大家介紹了關于利用iOS實現(xiàn)實時檢測網(wǎng)絡狀態(tài)的相關資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-07-07
IOS中UITextView或UITextField字數(shù)限制的實現(xiàn)
這篇文章主要介紹了IOS中UITextView或UITextField字數(shù)限制的實現(xiàn)的相關資料,希望通過本文能幫助到大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10

