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];
}
- (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];
}
相关文章推荐
- 初探 iOS8 中的 Size Class
- IOS 在iphone中PopoverViewController的使用
- IOS TableViewCell分割线设置和隐藏多余cell
- iOS中文API之NSLayoutconstraint
- 蓝懿iOS 技术内容和心得 12.21
- iOS开发:同步方法中混杂异步方法,导致异步方法还没有回调,同步方法就返回了值,因此返回了nil。解决方法
- iOS 21种设计模式之原型模式Prototype
- iOS之动画总结
- iOS定位不调用代理方法的可能原因
- ios通知
- 为iOS应用提供技术支持
- 观察者模式 - 通知/KVO
- IOS沙盒Files目录说明和常用操作
- 在iOS中使用icon font
- iOS开发拓展篇—CoreLocation定位服务
- iOS屏幕尺寸和分辨率了解
- iOS中事件的传递和响应者链条
- 蓝懿IOS利用所学的控件做小游戏
- animation iOS实现动画
- IOS-自定义View 覆盖到Tabbar上, View不响应事件问题