UIButton 不同状态图片来回切换

做百度地图是否显示路况信息,用到两种状态的图片 

第一种方法:用Bool进行标记

声明一个 Bool _isRoad;

- (void)roadBtnAction:(UIButton *)sender

{

    if (!_isRoad) {

        [_roadBtn setBackgroundImage:[UIImage imageNamed:@"map_road_condition_sel"] forState:UIControlStateNormal];

        [_mapView setTrafficEnabled:YES];

    } else {

        [_roadBtn setBackgroundImage:[UIImage imageNamed:@"map_road_condition_nor"] forState:UIControlStateNormal];

        [_mapView setTrafficEnabled:NO];

    }

    _isRoad = !_isRoad;

}

第二种方法:用一个值进行标记

    @property (nonatomic, assign) NSInteger count;

     self.count = 0;

    _count++;

    if (_count % 2) {

        [_roadBtn setBackgroundImage:[UIImage imageNamed:@"map_road_condition_sel"] forState:UIControlStateNormal];

        [_mapView setTrafficEnabled:YES];

    } else {

        [_roadBtn setBackgroundImage:[UIImage imageNamed:@"map_road_condition_nor"] forState:UIControlStateNormal];

        [_mapView setTrafficEnabled:NO];

    }

posted @ 2015-10-13 16:47  fengkuangIT  阅读(576)  评论(0编辑  收藏  举报