欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • iOS開發(fā)底層探索界面優(yōu)化示例詳解

    iOS開發(fā)底層探索界面優(yōu)化示例詳解

    這篇文章主要為大家介紹了iOS開發(fā)底層探索界面優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • iOS實(shí)現(xiàn)拼圖小游戲

    iOS實(shí)現(xiàn)拼圖小游戲

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)拼圖小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • iOS下拉選擇菜單簡單封裝

    iOS下拉選擇菜單簡單封裝

    這篇文章主要為大家詳細(xì)介紹了iOS下拉選擇菜單封裝代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • iOS自帶原生二維碼掃描的實(shí)現(xiàn)

    iOS自帶原生二維碼掃描的實(shí)現(xiàn)

    最近項(xiàng)目中需要做一個(gè)二維碼掃描,雖然有很多二維碼掃描的第三方可以用,但是考慮到項(xiàng)目中的需要,所以我放棄了使用三方庫,而采用了蘋果原生的掃描。下面這篇文章就介紹了iOS自帶原生二維碼掃描的實(shí)現(xiàn),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • iOS實(shí)現(xiàn)實(shí)時(shí)檢測網(wǎng)絡(luò)狀態(tài)的示例代碼

    iOS實(shí)現(xiàn)實(shí)時(shí)檢測網(wǎng)絡(luò)狀態(tài)的示例代碼

    網(wǎng)絡(luò)連接狀態(tài)檢測對(duì)于我們的iOS開發(fā)來說是一個(gè)非常通用的需求。下面這篇文章主要就給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)實(shí)時(shí)檢測網(wǎng)絡(luò)狀態(tài)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-07-07
  • iOS開發(fā)實(shí)現(xiàn)簡單抽屜效果

    iOS開發(fā)實(shí)現(xiàn)簡單抽屜效果

    這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實(shí)現(xiàn)簡單抽屜效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • iOS微信分享后關(guān)閉發(fā)送成功提示并返回應(yīng)用

    iOS微信分享后關(guān)閉發(fā)送成功提示并返回應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了iOS微信分享后關(guān)閉發(fā)送成功提示并返回應(yīng)用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS本地動(dòng)態(tài)生成驗(yàn)證碼的方法

    iOS本地動(dòng)態(tài)生成驗(yàn)證碼的方法

    這篇文章主要介紹了iOS本地動(dòng)態(tài)生成驗(yàn)證碼的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-01-01
  • IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)

    IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)

    這篇文章主要介紹了IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-10-10
  • iOS中狀態(tài)欄的基本使用方法匯總

    iOS中狀態(tài)欄的基本使用方法匯總

    在iOS開發(fā)過程中,經(jīng)常會(huì)設(shè)置狀態(tài)欄的樣式,所以下面這篇文章主要給大家介紹了關(guān)于iOS中狀態(tài)欄的基本使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-07-07

最新評(píng)論