IOS开发小技巧 - 去除button点击高亮效果

为button设置背景图片,默认会有点击闪烁的效果,这个效果是通过button 的highlighted属性来控制的,点击button的时候highlighted为YES,松开后highlighted为NO.所以只要在点击事件处理方法中将highlighted设置为NO就可以了

示例如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor cyanColor];
    [button setTitle:@"+" forState:UIControlStateNormal];
    button.frame = CGRectMake(100, 64, 100, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"btn_bg"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    //保证所有touch事件button的highlighted属性为NO,即可去除高亮效果
    [button addTarget:self action:@selector(preventFlicker:) forControlEvents:UIControlEventAllTouchEvents];
}

- (void)buttonTapped:(UIButton *)button {
    NSLog(@"touch up inside");
}

- (void)preventFlicker:(UIButton *)button {
    button.highlighted = NO;
}

你可能感兴趣的:(IOS开发小技巧 - 去除button点击高亮效果)