UIButton忽略内容模式突出显示(adjustImageWhenHighlighted)

前端之家收集整理的这篇文章主要介绍了UIButton忽略内容模式突出显示(adjustImageWhenHighlighted)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用[myButton set Image:forState:]为UIButton设置了一个UIImage;
我设置它的contentMode使用[[myButton imageView] setContentMode:UIViewContentModeScaleAspectFit];
但是当您点击按钮时,它将返回到UIViewContentModeScaleToFill并将我的图像拉出.

使用adjustImageWhenHighlighted修复这个,但是然后我放宽了变暗的效果,我想保留.

有什么建议如何应付这个?

解决方法

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];        
    }];
}
原文链接:https://www.f2er.com/iOS/336028.html

猜你在找的iOS相关文章