iOS實(shí)現(xiàn)簡單分欄效果
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)簡單分欄效果的具體代碼,供大家參考,具體內(nèi)容如下
直接貼代碼嘍
GMSubfieldViiew.h
#import <UIKit/UIKit.h> @interface GMSubfieldViiew : UIView /** ?* select index ?*/ @property(nonatomic,copy) void(^clickIndex)(NSInteger index); - (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)thiTitles; /** ?* ?默認(rèn)勾選 ?*/ @property(nonatomic,assign) NSInteger selectedIndex; @end
GMSubfieldViiew.m
#import "GMSubfieldViiew.h"
#define lineH 2
@interface GMSubfieldViiew ()
/**
?* ?titles
?*/
@property(nonatomic,strong) NSArray * titles;
/**
?* ?lineView
?*/
@property(nonatomic,weak) UIView *lineView;
/**
?* ?itemWidth
?*/
@property(nonatomic,assign) CGFloat itemWidth;
@end
@implementation GMSubfieldViiew
#pragma mark - initUI
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)thiTitles
{
? ? if (self = [super initWithFrame:frame]) {
? ? ? ? self.titles = thiTitles;
? ? ? ? //initSubViews
? ? ? ? [self initSubViews];
? ? }
? ? return self;
}
#pragma mark - action
- (void) initSubViews
{
? ? self.itemWidth = kScreen_Width/self.titles.count;
? ? //add child
? ? for (int i=0; i<self.titles.count; i++) {
? ? ? ? UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
? ? ? ? [btn setTitle:self.titles[i] forState:UIControlStateNormal];
? ? ? ? btn.titleLabel.font = FontSize(15);
? ? ? ? btn.tag ?= 100+i;
? ? ? ? btn.layer.borderWidth = 0.5;
? ? ? ? [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
? ? ? ? btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
? ? ? ? [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? [self addSubview:btn];
? ? }
? ? //添加下劃線
? ? UIView *lineView ?= [[UIView alloc]init];
? ? lineView.backgroundColor = [UIColor blackColor];
? ? [self addSubview:lineView];
? ? self.lineView ? ? = lineView;
}
- (void)layoutSubviews
{
? ? [super layoutSubviews];
? ? for (int i=0; i<self.titles.count; i++) {
? ? ? ? UIButton *btn = [self viewWithTag:100+i];
? ? ? ? btn.frame = CGRectMake(i*self.itemWidth, 0, self.itemWidth, self.bounds.size.height-lineH+1);
? ? }
? ? self.lineView.frame = CGRectMake(self.selectedIndex*self.itemWidth, self.bounds.size.height-lineH, self.itemWidth, lineH);
}
- (void) btnClick:(UIButton *)btn
{
? ? NSInteger index = btn.tag -100;
? ? ESWeakSelf
? ? [UIView animateWithDuration:0.2 animations:^{
? ? ? ? ESStrongSelf
? ? ? ? self.lineView.frame = CGRectMake(index*self.itemWidth, self.bounds.size.height-lineH, self.itemWidth, lineH);
? ? }];
? ? if (self.clickIndex) {
? ? ? ? self.clickIndex(index);
? ? }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
? ? // Drawing code
}
*/
@end調(diào)用:
GMSubfieldViiew *segView = [[GMSubfieldViiew alloc]initWithFrame:CGRectMake(0, 10, kScreen_Width, segH) titles:@[@"未還",@"已還"]];
? ? segView.selectedIndex = 1;
? ? ESWeakSelf
? ? segView.clickIndex = ^(NSInteger index){
? ? ? ? self.isHK = NO;
? ? ? ? ESStrongSelf
? ? ? ? if(index==0){
? ? ? ? ? ? //未還
? ? ? ? ? ? self.rightButton.hidden = NO;
? ? ? ? }
? ? ? ? else if(index==1){
? ? ? ? ? ?//已還
? ? ? ? ? ? self.rightButton.hidden = YES;
? ? ? ? ? ? self.containView.hidden = YES;
? ? ? ? }
? ? ? ? self.tableView.frame = CGRectMake(0, 60, kScreen_Width, kScreen_Height-NavHeight-60);
? ? ? ? [self.tableView reloadData];
? ? };
? ?[self.view addSubView:segView];效果圖:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS發(fā)送驗(yàn)證碼倒計(jì)時(shí)應(yīng)用
點(diǎn)擊發(fā)送驗(yàn)證碼button。倒計(jì)時(shí)開始,這篇文章就為大家詳細(xì)介紹了iOS發(fā)送驗(yàn)證碼倒計(jì)時(shí)應(yīng)用的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
利用iOS動(dòng)畫來模擬音量振動(dòng)條的實(shí)現(xiàn)
本篇文章主要利用iOS動(dòng)畫來模擬音量振動(dòng)條的實(shí)現(xiàn)以及對(duì)CAReplicatorLayer的簡單介紹,需要的朋友可以參考下2015-07-07
IOS Cache設(shè)計(jì)詳細(xì)介紹及簡單示例
這篇文章主要介紹了IOS Cache設(shè)計(jì)詳細(xì)介紹及簡單示例的相關(guān)資料,Cache的目的是為了追求更高的速度體驗(yàn),Cache的源頭是兩種數(shù)據(jù)讀取方式在成本和性能上的差異,需要的朋友可以參考下2017-01-01
iOS App中調(diào)用相冊(cè)中圖片及獲取最近的一張圖片的方法
這篇文章主要介紹了iOS App中調(diào)用相冊(cè)中圖片及獲取最近的一張圖片的方法,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03
iOS自定義字體設(shè)置和系統(tǒng)自帶的字體詳解
這篇文章主要給大家介紹了關(guān)于iOS自定義字體設(shè)置和系統(tǒng)自帶的字體的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
iOS設(shè)置圓角的4種方法實(shí)例(附性能評(píng)測)
這篇文章主要給大家介紹了關(guān)于iOS設(shè)置圓角的4種方法,并給大家附上了性能評(píng)測,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01

