@interface ViewController ()
@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)UIButton *fBtn;
@property(nonatomic,strong)NSArray *picarray;
@property(nonatomic,strong)NSArray *picxuan;
@property(nonatomic,strong)UIImage *image;
@property(nonatomic,strong)UIButton *selectedBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloatbtnwidth =110;
CGFloatbtnheight =42;
CGFloatbtnspace = (width-4*btnwidth)/5;
_picarray = @[@"shang",@"niuyue",@"zhenZhou",@"Dalian"];
_picxuan = @[@"shangS",@"niuyueS",@"zhenZhouS",@"DalianS"];
for(inti=0; i<4; i++) {
_button= [[UIButtonalloc]init];
_button.frame=CGRectMake(btnspace+(btnwidth+btnspace)*i,50, btnwidth, btnheight);
_button.tag= i;
[_button addTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];
if(_button.tag==0) {
[_button setImage:[UIImage imageNamed:_picxuan[i]] forState:UIControlStateNormal];
_selectedBtn = _button;
}else{
// _button.backgroundColor = [UIColor colorWithPatternImage:[UIColor colorWithPatternImage:_picarray[i]]];
//_button.backgroundColor = [UIColor whiteColor];
// _button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:_picarray[i]]];
[_button setImage:[UIImage imageNamed:_picarray[i]] forState:UIControlStateNormal];
}
[self.viewaddSubview:_button];
}
}
-(void)clickbtn:(UIButton*)btn{
if (_selectedBtn) {
NSString*str = [NSStringstringWithFormat:@"%ld",btn.tag-1];
if([strisEqualToString:@"-1"]) {
[_selectedBtn setImage:[UIImage imageNamed:_picarray[0]] forState:UIControlStateNormal];
}else{
[_selectedBtn setImage:[UIImage imageNamed:_picarray[btn.tag-1]] forState:UIControlStateNormal];
}
}
_selectedBtn = btn;
[_selectedBtn setImage:[UIImage imageNamed:_picxuan[btn.tag]] forState:UIControlStateNormal];
}
@end
网友评论