日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

iOS?Segment帶滑動條切換效果_IOS

作者:長沙火山 ? 更新時間: 2022-05-23 編程語言

本文實例為大家分享了iOS Segment帶滑動條切換效果的具體代碼,供大家參考,具體內容如下

#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:@"標題1",@"標題2",@"標題3",@"標題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];
}
?
//更新按鈕點擊效果
- (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

原文鏈接:https://blog.csdn.net/u010545480/article/details/47428535

欄目分類
最近更新