我自己常用的循环按钮笨方法

- (void)brandHistoryButton

{

    buttonsArray = [[NSMutableArray alloc]init]; //button放到数组里面

    for (int i=0; i<6; i++) {

        UIButton *button = [[UIButton alloc]init];

        button.frame = CGRectMake(93, 219+(i*46), 103, 35);

//        button.backgroundColor = [UIColor blackColor];

        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"BrandButton%02d",i+1]] forState:UIControlStateNormal];

        [button addTarget:self action:@selector(BrandHistoryBtnEvent:) forControlEvents:UIControlEventTouchUpInside];

        button.tag = i+200;

        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"huang_line"]];

        imageView.frame = CGRectMake(0, CGRectGetMaxY(button.frame), CGRectGetWidth(self.LeftBrandView.frame), imageView.image.size.height/2);

        [self.LeftBrandView addSubview:button];

        [self.LeftBrandView addSubview:imageView];

        [buttonsArray addObject:button];

//        NSLog(@"-o-o-%f    %f-o-o-",imageView.frame.origin.y,imageView.frame.size.height);

    }

    ((UIButton *)[buttonsArray objectAtIndex:0]).selected=YES// 关键是这里,设置 数组的第一个button为选中状态

    

    NSLog(@"%@",buttonsArray);

    

    UIImageView *iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guomu"]];

    iconImageView.frame = CGRectMake(28, 576, iconImageView.image.size.width, iconImageView.image.size.height);

    [self.LeftBrandView addSubview:iconImageView];

}

- (void)ButtonState:(UIButton *)button

{

    if (_tmpBtn == nil){

        button.selected = YES;

        _tmpBtn = button;

    }

    else if (_tmpBtn !=nil && _tmpBtn == button){

        button.selected = YES;

        

    }

    else if (_tmpBtn!= button && _tmpBtn!=nil){

        _tmpBtn.selected = NO;

        button.selected = YES;

        _tmpBtn = button;

    }

}

- (void)BrandHistoryBtnEvent:(UIButton *)button

{

    ((UIButton *)[buttonsArray objectAtIndex:0]).selected=NO;

    [self ButtonState:button];

    

    switch (button.tag) {

        case 200:

            NSLog(@"200");

            self.RightBrandView.hidden = NO;

            [self moveLeftView];

            break;

        case 201:

            NSLog(@"201");

            self.RightBrandView.hidden = YES;

            [self moveLeftView];

            break;

        case 202:

            NSLog(@"202");

            [self moveLeftView];

            break;

        case 203:

            NSLog(@"203");

            [self moveLeftView];

            break;

        case 204:

            NSLog(@"204");

            [self moveLeftView];

            break;

        case 205:

            NSLog(@"205");

            [self moveLeftView];

            break;

    }

    if (_tmpBtn.selected == YES) {

        [self buttonMark];

    }

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值