您的位置:首页 > 移动开发 > IOS开发

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

2015-12-21 23:56 543 查看
本例子主要是生成五个不同颜色的圆形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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: