关于UIBotton的高亮显示图片的一个小问题

时间:2022-02-28 17:36:18
关于UIBotton的高亮显示图片的一个小问题
下面这短代码是我建立的一个UIBotton : deleteButton

        // 删除

deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(51, 5, 79, 23)];

deleteButton.backgroundColor = [UIColor clearColor];

[deleteButton setBackgroundImage:[UIImage imageNamed:@"Delete.png"] forState:UIControlStateNormal];

[deleteButton setImage:[UIImage imageNamed:@"Delete_e.png"] forState:UIControlStateHighlighted];

[deleteButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];

        deleteButton.enabled=NO;

[self addSubview:deleteButton];


假如将命令修改为:[deleteButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchDown];


那么Delete_e.png

这张图片 在点击高亮的时候,就不会替换背景显示出来.

另外附使用到的deletButton函数.
1.使deleteButton有效的一个方法:

- (void)closeChunk

{

if(isChunkClosed)return;

[self performSelector:@selector(animaCloseChunk:)];

if(newBlocks[selctNewBlockIndex].type==kBlockTypeSigned){

[deleteButton setBackgroundImage:[UIImage imageNamed:@"Delete_Green.png"] forState:UIControlStateNormal];

[deleteButton setImage:[UIImage imageNamed:@"Delete_Red.png"] forState:UIControlStateHighlighted];

deleteButton.enabled=YES;

}else{

[deleteButton setBackgroundImage:[UIImage imageNamed:@"Delete.png"] forState:UIControlStateNormal];

[deleteButton setImage:[UIImage imageNamed:@"Delete.png"] forState:UIControlStateHighlighted];

deleteButton.enabled=NO;

}

}

2.deletButton方法:

- (void)delete:(id)sender{

[deleteButton setBackgroundImage:[UIImage imageNamed:@"Delete.png"] forState:UIControlStateNormal];

[deleteButton setImage:[UIImage imageNamed:@"Delete.png"] forState:UIControlStateHighlighted];

deleteButton.enabled=NO;

}