gpt4 book ai didi

image - UIButton 在突出显示时忽略 contentMode(adjustsImageWhenHighlighted)

转载 作者:技术小花猫 更新时间:2023-10-29 10:38:48 25 4
gpt4 key购买 nike

我使用 [myButton setImage:forState:]; 为我的 UIButton 设置了一个 UIImage我使用 [[myButton imageView] setContentMode:UIViewContentModeScaleAspectFit]; 将其设置为 contentMode但是当你点击按钮时,它会返回到 UIViewContentModeScaleToFill 并拉伸(stretch)我的图像。

使用 adjustsImageWhenHighlighted 解决了这个问题,但后来我失去了变暗效果,我想保留它。

关于如何处理这个问题有什么建议吗?

最佳答案

UIButton *imageBtn = [UIButton ...
imageBtn.adjustsImageWhenHighlighted = NO;

[imageBtn addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];

[imageBtn addTarget:self action:@selector(doHighlighted:) forControlEvents:UIControlEventTouchDown];
[imageBtn addTarget:self action:@selector(doHighlighted:) forControlEvents:UIControlEventTouchDragEnter];
[imageBtn addTarget:self action:@selector(doCancelHighlighted:) forControlEvents:UIControlEventTouchDragExit];

-(void)doSomething:(UIButton *)button{
...
[self performSelector:@selector(doCancelHighlighted:) withObject:button afterDelay:0.2f];
}

-(void)doHighlighted:(UIButton *)button{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 300, 300)];
imageView.backgroundColor = [UIColor blackColor];
imageView.alpha = 0.7;
imageView.tag = 1000;
[button addSubview:imageView];
}

-(void)doCancelHighlighted:(UIButton *)button{
UIView *view = [button subviewWithTag:1000];
[UIView animateWithDuration:0.2f animations:^{
view.alpha = 0;
} completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}

关于image - UIButton 在突出显示时忽略 contentMode(adjustsImageWhenHighlighted),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4420171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com