iOS开发-UI高级 选中按钮的实现方法总结
2016-08-21 10:56
567 查看
方法一:
UIView的类目:
设置按钮:
<strong><span style="font-size:18px;">// 标题按钮的响应事件
#pragma mark - 标题按钮的响应事件
-(void)titleBtnAction:(UIButton *)button{
// 切换按钮的选中状态
self.selectedBtn.enabled = YES;
button.enabled = NO;
self.selectedBtn = button;
// 下标指示视图的移动
[UIView animateWithDuration:0.2 animations:^{
self.suffixView.width = button.titleLabel.width;
self.suffixView.center = button.center;
}];
}</span></strong>
方法二:
UIView的类目:
<strong><span style="font-size:18px;">-(void)setCenterX:(CGFloat)centerX{ CGPoint center = self.center; center.x = centerX; self.center = center; } -(void)setCenterY:(CGFloat)centerY{ CGPoint center = self.center; center.y = centerY; self.center = center; } -(CGFloat)centerX{ return self.center.x; } -(CGFloat)centerY{ return self.center.y; } </span></strong>
设置按钮:
<strong><span style="font-size:18px;">#import "ViewController.h" @interface ViewController () // 下标指示视图属性 @property(nonatomic,strong)UIView *suffixView; // 选中按钮属性 @property(nonatomic,strong)UIButton *selectedBtn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 数组的设置 NSArray *titleArray = @[@"全部",@"视频",@"娱乐",@"图片",@"新闻"]; CGFloat titleBtnX = self.view.frame.size.width/5; CGFloat titleBtnY = 64; CGFloat titleBtnWidth = self.view.frame.size.width/5; CGFloat titleBtnHeight = 35; for (int i = 0; i < 5; i++) { // titleBtn的初始化 UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // titleBtn的frame设置 titleBtn.frame = CGRectMake(titleBtnX * i, titleBtnY, titleBtnWidth, titleBtnHeight); // 设置titleBtn按钮的标题 [titleBtn setTitle:titleArray[i] forState:UIControlStateNormal]; // 正常状态的标题颜色 [titleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; // 不能使用(选中)状态的标题颜色 [titleBtn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled]; // 按钮的字体大小设置 titleBtn.titleLabel.font = [UIFont systemFontOfSize:14]; [titleBtn addTarget:self action:@selector(titleBtnAction:) forControlEvents:UIControlEventTouchUpInside]; // 添加到self.view [self.view addSubview:titleBtn]; // 默认选中第一个按钮 if (i == 0) { titleBtn.enabled = NO; self.selectedBtn = titleBtn; // 按钮大小自适应 [titleBtn.titleLabel sizeToFit]; // 下标视图的中心点和按钮的中心点一样 self.suffixView.width = titleBtn.titleLabel.width; self.suffixView.frame.center.x = titleBtn.center.x; } } }</span></strong>
<strong><span style="font-size:18px;">// 标题按钮的响应事件
#pragma mark - 标题按钮的响应事件
-(void)titleBtnAction:(UIButton *)button{
// 切换按钮的选中状态
self.selectedBtn.enabled = YES;
button.enabled = NO;
self.selectedBtn = button;
// 下标指示视图的移动
[UIView animateWithDuration:0.2 animations:^{
self.suffixView.width = button.titleLabel.width;
self.suffixView.center = button.center;
}];
}</span></strong>
方法二:
<strong><span style="font-size:18px;">- (void)customTabBar{ // 标题数组 NSArray *titles = @[@"首页",@"新闻",@"Top",@"影院",@"更多"]; // 图片数组 NSArray *imageNames =@[@"movie_home",@"msg_new",@"start_top250",@"icon_cinema",@"more_setting"]; // 按钮的宽度 float buttonWidth = [UIScreen mainScreen].bounds.size.width/titles.count; // 选中图片初始化 selectImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; selectImageV.image = [UIImage imageNamed:@"selectTabbar_bg_all"]; // 添加到self.tabBar [self.tabBar addSubview:selectImageV]; // 循环添加按钮标题和图片 for (int i = 0; i < titles.count; i++) { NSString *title = titles[i]; NSString *imageName = imageNames[i]; CGRect frame = CGRectMake(i*buttonWidth, 0, buttonWidth, 49); // 初始化按钮 TabBarButton *button = [[TabBarButton alloc] initWithFrame:frame withImageName:imageName withTitle:title]; // 设置tag值 button.tag = 1000 + i; // 按钮的响应事件 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; // 默认选中第一个按钮 if (i == 0) { selectImageV.center = button.center; } // 添加到self.tabBar [self.tabBar addSubview:button]; } } #pragma mark - 按钮的响应事件 - (void) buttonAction: (UIButton *)btn { // 选中按钮的下标 self.selectedIndex = btn.tag - 1000; // 选中图片的中心点等于选中按钮的中心点 [UIView animateWithDuration:0.3 animations:^{ selectImageV.center = btn.center; }]; } </span></strong>注意:这里只总结了两种常用方法
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法