今天给大家做个两个互斥效果,废话不说 直接上代码
创建两个全局按钮 其中两个按钮的触发方法一样
@property (nonatomic,strong)UIButton *normalBtn;
@property (nonatomic,strong)UIButton *fastBtn;
self.normalBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.normalBtn setImage:[UIImage imageNamed:@"show_Car"] forState:0];
[self.normalBtn setImage:[UIImage imageNamed:@"hidden_Car"] forState:UIControlStateSelected];
[self.normalBtn addTarget:self action:@selector(btnTouch:) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:self.normalBtn];
self.fastBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
[self.fastBtn setImage:[UIImage imageNamed:@"hidden_Parking"] forState:0];
[self.fastBtn setImage:[UIImage imageNamed:@"show_Parking"] forState:UIControlStateSelected];
[self.fastBtn addTarget:self action:@selector(btnTouch:) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:self.fastBtn];
下面就是触发方法
-(void)btnTouch:(UIButton*)sender
{
if(sender.selected==YES) {
self.fastBtn.selected= !self.fastBtn.selected;
}else{
sender.selected= !sender.selected;
}
self.normalBtn.selected = !self.normalBtn.selected;
}
这样两个完美的互斥效果的按钮就完成了 是不是很简单
网友评论