iOS?Segment帶滑動(dòng)條切換效果
更新時(shí)間:2022年03月21日 11:06:09 作者:長(zhǎng)沙火山
這篇文章主要為大家詳細(xì)介紹了iOS?Segment帶滑動(dòng)條切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了iOS Segment帶滑動(dòng)條切換效果的具體代碼,供大家參考,具體內(nèi)容如下
#import "ViewController.h"
?
@interface ViewController ()
?
@property (nonatomic,strong) NSArray *arrTitle;
?
@property (nonatomic,strong) UIView *flyBar;
?
@end
?
@implementation ViewController
?
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view, typically from a nib.
? ??
? ? _arrTitle = [[NSArray alloc] initWithObjects:@"標(biāo)題1",@"標(biāo)題2",@"標(biāo)題3",@"標(biāo)題4", nil];
? ??
? ? UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
? ? baseView.backgroundColor = [UIColor orangeColor];
? ? [self.view addSubview:baseView];
? ??
? ? for (int i=0; i<_arrTitle.count; i++) {
? ? ? ? UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/_arrTitle.count*i, 20,self.view.frame.size.width/_arrTitle.count, 40)];
? ? ? ? [btn setTitle:[_arrTitle objectAtIndex:i] forState:UIControlStateNormal];
? ? ? ? [btn setTag:100+i];
? ? ? ? [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? [baseView addSubview:btn];
? ? }
? ??
? ? _flyBar = [[UIView alloc] initWithFrame:CGRectMake(0, baseView.frame.size.height-2, self.view.frame.size.width/_arrTitle.count, 2)];
? ? _flyBar.backgroundColor = [UIColor redColor];
? ? [baseView addSubview:_flyBar];
}
?
- (void)btnClick:(id)sender
{
? ? NSInteger tagNum = [sender tag];
? ? [self updateButtonClickState:tagNum];
}
?
//更新按鈕點(diǎn)擊效果
- (void)updateButtonClickState:(NSInteger)tagNum
{
? ? UIButton *currentBtn = (UIButton *)[self.view viewWithTag:tagNum];
? ??
? ? for (int i=100; i<_arrTitle.count+100; i++) {
? ? ? ? if (i != tagNum) {
? ? ? ? ? ? UIButton *btn = (UIButton *)[self.view viewWithTag:i];
? ? ? ? ? ? [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
? ? ? ? }
? ? }
? ??
? ? [UIView animateKeyframesWithDuration:0.1
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?delay:0.0
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?options:UIViewKeyframeAnimationOptionLayoutSubviews
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? animations:^{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? _flyBar.center = CGPointMake(currentBtn.center.x, _flyBar.center.y);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? completion:^(BOOL finished) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [currentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];
}
?
?
@end



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解iOS App開發(fā)中UIViewController的loadView方法使用
這篇文章主要介紹了詳解iOS App開發(fā)中UIViewController的loadView方法使用,講解了訪問view屬性時(shí)loadView方法的調(diào)用及使用loadView時(shí)的一些注意點(diǎn),需要的朋友可以參考下2016-03-03
ios UITableView 自定義右滑刪除的實(shí)現(xiàn)代碼
這篇文章主要介紹了ios UITableView 自定義右滑刪除的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法
這篇文章主要介紹了iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07
iOS應(yīng)用開發(fā)中運(yùn)用設(shè)計(jì)模式中的組合模式的實(shí)例解析
這篇文章主要介紹了iOS應(yīng)用開發(fā)中運(yùn)用設(shè)計(jì)模式中的組合模式的實(shí)例解析,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
詳解iOS應(yīng)用中播放本地視頻以及選取本地音頻的組件用法
這里來為大家詳解iOS應(yīng)用中播放本地視頻以及選取本地音頻的組件用法,分別使用MPMoviePlayerControlle和MPMediaPickerController來實(shí)現(xiàn),兩個(gè)都是MediaPlayer.framework中的多媒體組件,所以我們放到一起來講.2016-06-06

