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

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

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

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

解决方法

  1. UIButton *imageBtn = [UIButton ...
  2. imageBtn.adjustsImageWhenHighlighted = NO;
  3.  
  4. [imageBtn addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
  5.  
  6. [imageBtn addTarget:self action:@selector(doHighlighted:) forControlEvents:UIControlEventTouchDown];
  7. [imageBtn addTarget:self action:@selector(doHighlighted:) forControlEvents:UIControlEventTouchDragEnter];
  8. [imageBtn addTarget:self action:@selector(doCancelHighlighted:) forControlEvents:UIControlEventTouchDragExit];
  9.  
  10. -(void)doSomething:(UIButton *)button{
  11. ...
  12. [self performSelector:@selector(doCancelHighlighted:) withObject:button afterDelay:0.2f];
  13. }
  14.  
  15. -(void)doHighlighted:(UIButton *)button{
  16. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5,5,300,300)];
  17. imageView.backgroundColor = [UIColor blackColor];
  18. imageView.alpha = 0.7;
  19. imageView.tag = 1000;
  20. [button addSubview:imageView];
  21. }
  22.  
  23. -(void)doCancelHighlighted:(UIButton *)button{
  24. UIView *view = [button subviewWithTag:1000];
  25. [UIView animateWithDuration:0.2f animations:^{
  26. view.alpha = 0;
  27. } completion:^(BOOL finished) {
  28. [view removeFromSuperview];
  29. }];
  30. }

猜你在找的iOS相关文章