IOS 设置多个button,点击切换颜色

本例子主要是生成五个不同颜色的圆形button,并且每点击一次button就把当前的颜色值保存到本地,方便传输到其他地方使用。

- (void)viewDidLoad {

    [superviewDidLoad];

   //UIButton and changeColor  _array数组存放五组不同的颜色值

    _array = [[NSMutableArrayalloc]init];

    

    [_arrayaddObject:@[@0,@182.0,@147.0]];

    

    [_arrayaddObject:@[@92.0,@106.0,@233.0]];

    

    [_arrayaddObject:@[@2.0,@144.0,@255.0]];

    

    [_arrayaddObject:@[@46.0,@185.0,@230.0]];

    

    [_arrayaddObject:@[@97.0,@209.0,@253.0]];

//生成五个button

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

        

        UIButton *aBt = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        aBt.tag = i;

        _color = [UIColorcolorWithRed:[_array[i][0]floatValue]/255.0green:[_array[i][1]floatValue]/255.0blue:[_array[i][2]floatValue]/255.0alpha:1];

        aBt.backgroundColor = _color;

//        [aBt setBackgroundColor:[array objectAtIndex:i]];

//        UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom]];

//        UIImage *image = [UIImage imageNamed:@"xxx.png"];

//        [button setBackgroundImage:image forState:UIControlStateNormal];

        

        //设置边框宽度

        aBt.layer.borderWidth =1;

        aBt.layer.borderColor = [UIColorgreenColor].CGColor;

        aBt.layer.cornerRadius =25;

        aBt.layer.masksToBounds =YES;

        

        aBt.frame = CGRectMake(20+55*i,50, 50, 50);

        

//        aBt.backgroundColor = [UIColor blueColor];

        

        [aBt addTarget:selfaction:@selector(changeColor:)forControlEvents:UIControlEventTouchUpInside];

        

        [self.viewaddSubview:aBt];

    }

    //@"saveColor" 判断本地是否有颜色值,有的话直接给背景赋值

    NSMutableArray *saveArr = [[NSUserDefaultsstandardUserDefaults]objectForKey:@"saveColor"];

    if (saveArr) {

        self.view.backgroundColor = [UIColorcolorWithRed:[saveArr[0]floatValue]/255.0green:[saveArr[1]floatValue]/255.0blue:[saveArr[2]floatValue]/255.0alpha:1];

    }

    

}

#pragma mark - ChangeColor method 改变颜色的方法并利用NSUserDefaults保存到本地

-(void)changeColor:(UIButton*)button{

    NSLog(@"button tag is %ld",(long)button.tag);

   _color = [UIColorcolorWithRed:[_array[button.tag][0]floatValue]/255.0green:[_array[button.tag][1]floatValue]/255.0blue:[_array[button.tag][2]floatValue]/255.0alpha:1];

    switch (button.tag) {

        case 0:

            [self.viewsetBackgroundColor:_color];

            break;

        case 1:

            [self.viewsetBackgroundColor:_color];

            break;

        case 2:

            [self.viewsetBackgroundColor:_color];

            break;

        case 3:

            [self.viewsetBackgroundColor:_color];

            break;

        case 4:

            [self.viewsetBackgroundColor:_color];

            break;

        default:

            break;

    }

    [[NSUserDefaults standardUserDefaults]setObject:_array[button.tag]forKey:@"saveColor"];

    [[NSUserDefaults standardUserDefaults]synchronize];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值