最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS UISegmentControl實(shí)現(xiàn)自定義分欄效果

 更新時(shí)間:2022年03月21日 11:41:46   作者:lalu  于 2019-02-25 23:49:34 發(fā)布  3851  
這篇文章主要為大家詳細(xì)介紹了iOS UISegmentControl實(shí)現(xiàn)自定義分欄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

iOS 自帶的UISegmentControl實(shí)現(xiàn)的就是類似上圖的效果
但是很多用處 一般這兩個(gè)分欄是兩個(gè)tableView,需要左右滑動(dòng)來響應(yīng)分欄

下面來講述這樣的效果是怎么實(shí)現(xiàn)的呢?

主要那里有一根短紅線,需要滑動(dòng) 來切換tableView

先自定義一個(gè)SegmentView

.h

//定義block,用來傳遞點(diǎn)擊的第幾個(gè)按鈕
typedef void (^PassValueBlock)(NSInteger index);

@interface BCLCommunitySegmentView : UIView

//定義一下block
@property (nonatomic, strong) PassValueBlock returnBlock;
@property (nonatomic, strong) UIImageView *selectImage;//這個(gè)就是短紅線

//初始化數(shù)組,傳入frame和名稱
- (id) initWithFrame:(CGRect)frame withTitleArray:(NSArray *)array;

//block傳遞值方法
- (void)setReturnBlock:(PassValueBlock)returnBlock;
@end

在SegmentView.m中
循環(huán)創(chuàng)建按鈕,幾個(gè)分欄創(chuàng)建幾個(gè)按鈕

- (void)creatSegmentView {
? ? //設(shè)置按鈕的寬度
? ? _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];
? ? ? ??
? ? ? ? //設(shè)置button的字
? ? ? ? [button setTitle:_titleArray[i] forState:UIControlStateNormal];
? ? ? ? //設(shè)置button的字顏色
? ? ? ??
? ? ? ? [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
? ? ? ? //設(shè)置字體大小
? ? ? ? button.titleLabel.font = [UIFont systemFontOfSize:20];
? ? ? ? //設(shè)置居中顯示
? ? ? ? button.titleLabel.textAlignment = NSTextAlignmentCenter;
? ? ? ? //設(shè)置tag值
? ? ? ? button.tag = 1000 + i;
? ? ? ? //添加點(diǎn)擊事件
? ? ? ? [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? //如果是第一個(gè),默認(rèn)被選中
? ? ? ? if (i == 0) {
? ? ? ? ? ? button.selected = YES;
? ? ? ? }
? ? }
? ??
? ??
? ? //添加一個(gè)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];
}

然后設(shè)置按鈕的點(diǎn)擊事件,將點(diǎn)擊到哪個(gè)按鈕 回調(diào)過去

-(void)buttonAction:(UIButton *)button{
? ??
? ? //當(dāng)button被點(diǎn)擊,所有的button都設(shè)為未選中狀態(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];
? ? ? ? }
? ? }
? ? //然后將選中的這個(gè)button變?yōu)檫x中狀態(tài)
? ? button.selected = YES;
? ??
? ? //通過當(dāng)前的tag值設(shè)置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實(shí)現(xiàn)分欄的兩個(gè)tableView的滑動(dòng)

- (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代理 滑動(dòng)scrollerView實(shí)現(xiàn)小紅條的滑動(dòng)

- (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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論

桦甸市| 安庆市| 昌乐县| 广南县| 阳泉市| 石林| 泗洪县| 盐源县| 舞阳县| 休宁县| 广南县| 霍城县| 营山县| 本溪市| 交城县| 应用必备| 中卫市| 古田县| 苗栗县| 广宁县| 岑巩县| 竹北市| 河曲县| 满洲里市| 汕尾市| 宁强县| 比如县| 苗栗市| 岳阳县| 奉化市| 桦甸市| 城口县| 遂平县| 大丰市| 定兴县| 仁布县| 永年县| 武夷山市| 乐业县| 哈尔滨市| 盐城市|