我使用[myButton set
Image:forState:]为UIButton设置了一个UIImage;
我设置它的contentMode使用[[myButton imageView] setContentMode:UIViewContentModeScaleAspectFit];
但是当您点击按钮时,它将返回到UIViewContentModeScaleToFill并将我的图像拉出.
我设置它的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];
- }];
- }