iOS之点击按钮切换背景图片

现在的项目的按钮切换的时候,都会添加一个效果,就是点击后更换一个背景图片,而原来被点击的按钮的图片恢复原来的背景图片

首先,在.h文件中写一个BOOL的属性,

@property(nonatomic ,strong) UIButton *landButton;
@property(nonatomic ,strong) UIButton * weiXinButton;
@property(nonatomic ,strong) UIButton * phoneButton;
@property(nonatomic ,assign) BOOL isClick;

然后在.m文件中:

- (void)viewDidLoad
{
    [super viewDidLoad];
   //self.view.backgroundColor = [UIColor redColor];
    _landButton = [UIButton buttonWithType:UIButtonTypeCustom];
         _landButton.frame = CGRectMake(150, 100, 60, 60);
    [_landButton setBackgroundImage:[UIImage imageNamed:@"after.jpg"] forState:UIControlStateNormal];
    _landButton.tag = 110;
    [_landButton addTarget:self action:@selector(aa:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_landButton];
    
    _weiXinButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _weiXinButton.frame = CGRectMake(150, 200, 60, 60);
    [_weiXinButton setBackgroundImage:[UIImage imageNamed:@"before.jpg"] forState:UIControlStateNormal];
    [_weiXinButton addTarget:self action:@selector(aa:) forControlEvents:UIControlEventTouchUpInside];
    _weiXinButton.tag = 111;
    [self.view addSubview:_weiXinButton];
    
    _phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _phoneButton.frame = CGRectMake(150, 300, 60, 60);
    [_phoneButton setBackgroundImage:[UIImage imageNamed:@"before.jpg"] forState:UIControlStateNormal];
    [_phoneButton addTarget:self action:@selector(aa:) forControlEvents:UIControlEventTouchUpInside];
    _phoneButton.tag = 112;
    [self.view addSubview:_phoneButton];
}

//点击按钮时触发的事件,思想就是,点击按钮时按钮更换背景图片,而其他图片恢复原始状态

-(void)changeBackImage:(UIButton *)sender

{
    _isClick =YES;
    
    switch (sender.tag) {
        case 110:
        {
            [_landButton setBackgroundImage: [UIImage imageNamed:@"after.jpg"] forState:UIControlStateNormal];
            [_weiXinButton setBackgroundImage: [UIImage imageNamed:@"beafore.jpg"] forState:UIControlStateNormal];
            [_phoneButton setBackgroundImage: [UIImage imageNamed:@"before.jpg"] forState:UIControlStateNormal];


        }
            break;
        case 111:
        {
            _isClick =YES;
            [_weiXinButton setBackgroundImage: [UIImage imageNamed:@"after.jpg"] forState:UIControlStateNormal];
            [_landButton setBackgroundImage: [UIImage imageNamed:@"before.jpg"] forState:UIControlStateNormal];
            [_phoneButton setBackgroundImage: [UIImage imageNamed:@"before.jpg"] forState:UIControlStateNormal];


        }
            break;
        case 112:
        {
            _isClick =YES;
            [_phoneButton setBackgroundImage: [UIImage imageNamed:@"after.jpg"] forState:UIControlStateNormal];
            [_landButton setBackgroundImage: [UIImage imageNamed:@"before.jpg"] forState:UIControlStateNormal];
            [_weiXinButton setBackgroundImage: [UIImage imageNamed:@"before.jpg"] forState:UIControlStateNormal];
        }
            break;
            
        default:
            break;
    }
    
    
}

你可能感兴趣的:(IOS)