ios中UIButton选中状态切换

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

添加响应事件:

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

在创建的时候就给定了正常时候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. }

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

ios中UIButton选中状态切换

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

  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. sender.selected = !sender.selected;
  8. }else{
  9. [btn setSelected:NO];
  10. }
  11. }
  12. }

效果图如下所示:
ios中UIButton选中状态切换