UIButton:用两张图片---代码中自己用的模版之二

我们在用UIButton的时候,想要两张图片,一张时正常显示的,一张是在点击时显示的,这个该怎么实现呢?

下面时我的实现方式,如有别的方式,请大家一起分享出来,共同学习....

    NSString *path = nil;
    UIImage *image = nil;    
    path = [[NSBundle mainBundle] pathForResource:@"menubtn1" ofType:@"png"];
    image = [UIImage imageWithContentsOfFile:path];
    _gameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _gameBtn.frame = CGRectMake(180, 670, image.size.width, image.size.height);
    [_gameBtn setImage:image forState:UIControlStateNormal];//这个图片是在布点击时响应
    path = nil;
    image = nil;
    
    path = [[NSBundle mainBundle] pathForResource:@"menubtn1_1" ofType:@"png"];
    image = [UIImage imageWithContentsOfFile:path];
    [_gameBtn setImage:image forState:UIControlStateHighlighted];//图片在点击的时候响应
    [_gameBtn addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_gameBtn];
    path = nil;
    image = nil;

下面还有响应事件的代码:

-(IBAction)ButtonClick:(id)sender{
    UIButton *whichOne = (UIButton *)sender;
    
    if (_gameBtn == whichOne) {
        NSLog(@"You Clicked _gameBtn");
    }
}


你可能感兴趣的:(image,path,action,iPad,iPhone开发,objective-c基础)