ios 在 UIImageView 上添加不透明度为 0.3 的黑色叠加层

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11217654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:58:55  来源:igfitidea点击:

adding black overlay with 0.3 opacity over UIImageView

iphoneobjective-ciosipadcalayer

提问by xonegirlz

I have a UIImageView and I wanted to add a black overlay on top of it. What is the best way of doing this without having to override drawRect? I was thinking of adding a CALayer on top of it. But unsure how to get a black CALayer with a .3 alpha.

我有一个 UIImageView,我想在它上面添加一个黑色覆盖层。在不必覆盖 drawRect 的情况下这样做的最佳方法是什么?我想在它上面添加一个 CALayer。但不确定如何获得 0.3 alpha 的黑色 CALayer。

回答by Mick MacCallum

Something like this?

像这样的东西?

UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myImageView.frame.size.width, myImageView.frame.size.height / 2)];
[overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
[myImageView addSubview:overlay];

回答by swiftBoy

Thanks to Mick MacCallum

感谢米克·麦卡勒姆

Swift 3Version

斯威夫特 3版本

let overlay: UIView = UIView(frame: CGRect(x: 0, y: 0, width: cell.imageView.frame.size.width, height: cell.imageView.frame.size.height))
overlay.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.1)
cell.imageView.addSubview(overlay)

I have used for Swift 2.2

我已经用于Swift 2.2

let overlay: UIView = UIView(frame: CGRectMake(0, 0, cell.imageView.frame.size.width, cell.imageView.frame.size.height))
overlay.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.1)
cell.imageView.addSubview(overlay)

回答by nfarshchi

The MDT answer is correct. This is just another way to use a CAGradientLayerbeside of UIView. I think it will make what you want with more graphical options.

MDT 答案是正确的。这只是另一种方式来使用CAGradientLayer的旁边UIView。我认为它将通过更多的图形选项满足您的需求。

first you should add

首先你应该添加

#import <QuartzCore/QuartzCore.h>

to your ViewController.mand any place that you want to add this overlay to your UIImageuse:

到您ViewController.m和您想要将此叠加层添加到您的UIImage用途的任何地方:

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = myImageView.layer.bounds;

gradientLayer.colors = [NSArray arrayWithObjects:
                        (id)[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor,
                        (id)[UIColor colorWithWhite:0.0f alpha:0.3f].CGColor,
                        nil];

gradientLayer.locations = [NSArray arrayWithObjects:
                           [NSNumber numberWithFloat:0.0f],
                           [NSNumber numberWithFloat:0.5f],
                           nil];

//If you want to have a border for this layer also
gradientLayer.borderColor = [UIColor colorWithWhite:1.0f alpha:1.0f].CGColor;
gradientLayer.borderWidth = 1;
[myImageView.layer addSublayer:gradientLayer];

I hope this will help you make it

我希望这会帮助你做到

回答by pasawaya

This is similar to MDT's answer except using CALayer properties:

除了使用 CALayer 属性外,这类似于 MDT 的答案:

UIView *blackOverlay = [[UIView alloc] initWithFrame: imageView.frame];
blackOverlay.layer.backgroundColor = [[UIColor blackColor] CGColor];
blackOverlay.layer.opacity = 0.3f;
[self.view addSubview: blackOverlay];

回答by DrummerB

Did you think about adding a UIViewwith black backgroundColor and 0.3 alpha as a subview to the image view?

你有没有想过UIView在图像视图中添加一个带有黑色背景颜色和 0.3 alpha 的子视图?