ios中UIButton选中状态切换

关于UIButton的事件枚举有许多,平时用的少所以很多的都不是很清楚,今天了解了下,看了以前的代码,觉得在UIButton选中时操作写了许多冗余代码,而忽略了UIButton一个很重要的属性,如下:

  1. typedef NS_OPTIONS(NSUInteger, UIControlState) {  
  2.     UIControlStateNormal       = 0,  
  3.     UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set  
  4.     UIControlStateDisabled     = 1 << 1,  
  5.     UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)  
  6. #ifndef SDK_HIDE_TIDE  
  7.     UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus  
  8. #endif  
  9.     UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use  
  10.     UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use  
  11. };  

中的UIControlStateSelected表示是否选中,NO表示未选中,YES表示选中;

1.这是之前写的:

创建UIButton,通过for 循环去创建

  1. //顶部view的初始化  
  2. - (void)initTopView{  
  3.       
  4.     topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 40)];  
  5.     topFrame = topView.frame;  
  6.     topView.backgroundColor = [UIColor whiteColor];  
  7.     topView.alpha = .8;  
  8.       
  9.     NSArray *titleArr = @[@"人气",@"价格",@"桌数",@"优惠"];  
  10.     for (int i = 0; i < titleArr.count; i ++) {  
  11.         UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
  12.         [btn setTitle:titleArr[i] forState:UIControlStateNormal];  
  13.         [btn setImage:[UIImage imageNamed:@"ph"] forState:UIControlStateNormal];  
  14.         [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];  
  15.         [btn setBackgroundImage:[UIImage imageNamed:@"button1"] forState:UIControlStateNormal];  
  16.         btn.titleLabel.font = [UIFont systemFontOfSize:12];  
  17.         btn.showsTouchWhenHighlighted = YES;  
  18.         btn.frame = CGRectMake(10 + i * ((kScreen_width - 50)/4 + 10) , 10, (kScreen_width - 50)/4, 25);  
  19.         //设置tag值  
  20.         btn.tag = i + 100;  
  21.         [btn addTarget:self action:@selector(choose:) forControlEvents:UIControlEventTouchUpInside];  
  22.         [topView addSubview:btn];  
  23.     }  
  24.       
  25.     [self.view addSubview:topView];  
  26. }  


添加响应事件:

  1. //人气、价格、作品数、优惠  
  2. - (void)choose:(UIButton *)sender{  
  3.     for (int i = 0; i < 4; i++) {  
  4.         UIButton *btn = (UIButton *)[[sender superview]viewWithTag:100 + i];  
  5.         [btn setSelected:NO];  
  6.         if (!btn.selected) {  
  7.             [btn setImage:[UIImage imageNamed:@"ph"] forState:UIControlStateNormal];  
  8.             [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];  
  9.             [btn setBackgroundImage:[UIImage imageNamed:@"button1"] forState:UIControlStateNormal];  
  10.         }  
  11.     }  
  12.     UIButton *button = (UIButton *)sender;  
  13.     [button setSelected:YES];  
  14.     [button setImage:[UIImage imageNamed:@"pho"] forState:UIControlStateNormal];  
  15.     [button setTitleColor:[UIColor colorWithRed:170.0/255 green:107.0/255 blue:208.0/255 alpha:1] forState:UIControlStateNormal];  
  16.     [button setBackgroundImage:[UIImage imageNamed:@"button2"] forState:UIControlStateNormal];  
  17. }  


这种是最简单的,相对也是最繁琐的,多了很多不必要的冗余代码,下面就让我们看看改进的;

2.通过使用UIButton自己的一个selected属性和normal属性重构的,如下所示

  1. -(void)initUIButtonView{  
  2.       
  3.     _titleArr = @[@"人气",@"价格",@"桌数",@"优惠"];  
  4.       
  5.     for (int i = 0; i < _titleArr.count; i ++) {  
  6.         UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
  7.         btn.frame = CGRectMake(10 + i * ((kScreen_width - 50)/4 + 10) , 20, (kScreen_width - 50)/4, 25);  
  8.         [btn setTitle:_titleArr[i] forState:UIControlStateNormal];  
  9.         btn.titleLabel.font = [UIFont systemFontOfSize:12];  
  10.         btn.showsTouchWhenHighlighted = YES;  
  11.         //设置tag值  
  12.         btn.tag = i + 100;  
  13.         btn.selected = NO;  
  14.         [btn addTarget:self action:@selector(choose:) forControlEvents:UIControlEventTouchUpInside];  
  15.         [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];  
  16.         [btn setImage:[UIImage imageNamed:@"ph"] forState:UIControlStateNormal];  
  17.         [btn setBackgroundImage:[UIImage imageNamed:@"button1"] forState:UIControlStateNormal];  
  18.   
  19.         [btn setTitleColor:[UIColor colorWithRed:170.0/255 green:107.0/255 blue:208.0/255 alpha:1] forState:UIControlStateSelected];  
  20.         [btn setImage:[UIImage imageNamed:@"pho"] forState:UIControlStateSelected];  
  21.         [btn setBackgroundImage:[UIImage imageNamed:@"button2"] forState:UIControlStateSelected];  
  22.         [self.view addSubview:btn];  
  23.     }  
  24.   
  25. }  

在创建的时候就给定了正常时候UIButton的样式,和选中UIButton时的按钮颜色,注意这里设置了默认的selected = NO;和UIControlStateSelected

在给定按钮选择事件,设置对应selected的状态值,如下所示:

  1. //人气、价格、作品数、优惠  
  2. - (void)choose:(UIButton *)sender{  
  3.     for (int i = 0; i < _titleArr.count; i++) {  
  4.         UIButton *btn = (UIButton *)[[sender superview]viewWithTag:100 + i];  
  5.         [btn setSelected:NO];  
  6.     }  
  7.     UIButton *button = (UIButton *)sender;  
  8.     [button setSelected:YES];  
  9. }  


这样看上去,第二种方法,是不是比第一种方法更简单明了,去除了相关的冗余代码的,效果如下所示


注:改进,上面的我们可以在不同的按钮上面切换状态,但在同一个按钮上面点击多次状态不会改变,针对上述问题做了些许的改动,其实主要是在点击事件里面,判断当前按钮的状态去改变,代码如下:

  1. //人气、价格、作品数、优惠  
  2. - (void)choose:(UIButton *)sender{  
  3.     for (int i = 0; i < _titleArr.count; i++) {  
  4.         UIButton *btn = (UIButton *)[[sender superview]viewWithTag:100 + i];  
  5.         //选中当前按钮时  
  6.         if (sender.tag == btn.tag) {  
  7.               
  8.             sender.selected = !sender.selected;  
  9.         }else{  
  10.               
  11.             [btn setSelected:NO];  
  12.         }  
  13.     }  
  14.   
  15. }  


效果图如下所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值