iOS Segment带滑动条切换效果
2015-08-11 22:17
134 查看
#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
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- iOS开发之路--微博OAuth授权_取得用户授权的accessToken
- ios通过按钮点击异步加载图片
- 加载带有手势识别器的XIB文件需注意哪些问题
- 仅需几行代码实现方便易用的状态栏指示器
- ios中图像进行压缩方法汇总