两个按钮切换
1.第一个按钮一开始就是选中状态,所以要调用[self btnClick:btn]方法。
2.如果点击了selected = YES的按钮,就什么都不做。
3.如果点击了selected = NO的按钮,就让当前点击的按钮selected = YES,另一个按钮的selected = NO。
// 按钮宽度
CGFloat btnWidth = (slideWidth - 60) / 2;
// 历史周刊按钮
RXBaseButton *weeklyMagaBtn = [RXBaseButton buttonWithType:UIButtonTypeCustom];
weeklyMagaBtn.tag = 200;
[weeklyMagaBtn setBackgroundImage:[UIImage imageNamed:@"历史周刊按钮"] forState:UIControlStateNormal];
[weeklyMagaBtn setBackgroundImage:[UIImage imageNamed:@"历史周刊选中"] forState:UIControlStateSelected];
[weeklyMagaBtn setTitle:@"历史周刊" forState:UIControlStateNormal];
weeklyMagaBtn.size = CGSizeMake(btnWidth , 40);
weeklyMagaBtn.x = 15;
weeklyMagaBtn.y = 54;
[self.view addSubview:weeklyMagaBtn];
self.weeklyMagaBtn = weeklyMagaBtn;
[weeklyMagaBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self btnClick:weeklyMagaBtn]; // 第一个按钮默认选中
// 类型分类按钮
RXBaseButton *typeMagaBtn = [RXBaseButton buttonWithType:UIButtonTypeCustom];
typeMagaBtn.tag = 201;
[typeMagaBtn setBackgroundImage:[UIImage imageNamed:@"历史周刊按钮"] forState:UIControlStateNormal];
[typeMagaBtn setBackgroundImage:[UIImage imageNamed:@"历史周刊选中"] forState:UIControlStateSelected];
[typeMagaBtn setTitle:@"类型分类" forState:UIControlStateNormal];
typeMagaBtn.size = CGSizeMake(btnWidth , 40);
typeMagaBtn.x = 45 + btnWidth;
typeMagaBtn.y = 54;
[self.view addSubview:typeMagaBtn];
self.typeMagaBtn = typeMagaBtn;
[typeMagaBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//RXLog(@"按钮的位置是%f,%f, %f", slideWidth, loginBtn.x, loginBtn.y);
// 点击按钮
-(void)btnClick:(UIButton *)btn这个方法为
if(btn.selected == NO){
if(btn.tag == 200){
self.typeMagaBtn.selected = NO;
}else{
self.weeklyMagaBtn.selected = NO;
}
btn.selected = YES;
}