I'm making a camera-based app and would like to make a frosted overlay on the camera preview, with a rounded rectangle cut out from the frosted view.
I've been trying to achieve this with the maskLayer of UIView, introduced with iOS 8 and have been wildly unsuccessful.
Here's what my code currently looks like:
// Blur the preview
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIView *blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
blurView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view insertSubview:blurView aboveSubview:imagePicker.view];
// Make blur view fill screen
{
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-0-[blurView]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(blurView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[blurView]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(blurView)]];
}
// Add a rounded of transparency to the blurView **not working**
UIView *mask = [[UIView alloc] initWithFrame:CGRectMake(50.f, 50.f, 50.f, 50.f)];
mask.alpha = 1.0f;
mask.backgroundColor = [UIColor redColor];
[blurView setMaskView:mask]; // Remove this line, and everything is frosted. Keeping this line and nothing is frosted.
As my comments indicate, adding the mask removes the frosted view completely (changing the opacity of the mask does nothing). I know that this won't create the effect I'm looking for (it should only be frosted in that rectangle), but I can't get the maskView to work in any capacity.
The UIView api says
An optional view whose alpha channel is used to mask a view’s
content.
The best way to see this is to use a UIImageView loaded with an RGBA image that sports an alpha channel - a PNG exported with transparency switched on from photoshop will do it. And then resize, with the origin set to 0,0.
A second way to do it would be to create a UIView, with no alpha i.e. [UIColor clearColor] and add a subview which has an alpha [UIColor whiteColor];
UIView *mask = [[UIView alloc] initWithFrame:(CGRect){0,0,100,100}];
mask.backgroundColor = [UIColor clearColor];
UIView *areaToReveal = [[UIView alloc] initWithFrame:(CGRect){20,20,50,50}];
areaToReveal.backgroundColor = [UIColor whiteColor];
[mask addSubview:areaToReveal];
_myView.maskView = mask;
You could add further UIViews which are translucent by x amount using [UIColor colorFromRed: green: blue: alpha:x];
Related
I have a UIView in which I define it's border in the following manner:
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 3;
I attach a subview to this UIView, and when I move the subview over the border, it goes underneath it. Is this the intended behavior? Is there anyway to make the subview go on top of it?
According to the Apple specification: It is composited above the receiver’s contents and sublayers.
So, the border will always be above of all your subviews, even if you bring your subview to the front and so on.
So I make a background view to fake the border.
E.g.:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.clipsToBounds = NO;
UIView *bView = [[UIView alloc] initWithFrame:CGRectInset(backgroundView.bounds, 3, 3)];
bView.backgroundColor = [UIColor redColor];
UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(-50, -50, 100, 100)];
cView.backgroundColor = [UIColor yellowColor];
[bView addSubview:cView];
[backgroundView addSubview:bView];
[self.window addSubview:backgroundView];
and the effect:
Depending on your view structure, it might be easier to add the subview to the parent of your main view. It can then overlap the main view and will overlay the border as you requested.
Did you try setting the superview's 'clipsToBounds' property to YES? This is set to NO by default for performance reasons, but setting it to yes might give you the effect you are looking for.
Insert layer at specific position that suits you:
self.layer.insertSublayer(sublayer, at: 0)
I make a circular imageView. Here is the code:
someImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2-35,80,80,80)];
[someImageView setContentMode:UIViewContentModeScaleAspectFit];
someImageView.layer.cornerRadius = someImageView.frame.size.height /2;
someImageView.layer.masksToBounds = YES;
someImageView.layer.borderWidth = 1.0;
someImageView.layer.borderColor = [UIColor colorWithRed:16.0f/255.0f
green:56.0f/255.0f
blue:70.0f/255.0f
alpha:1.0f].CGColor;
someImageView.clipsToBounds=YES;
[self.view addSubview:someImageView];
But when I make it hidden, I notice a rect(original size) for a very little time and then hidden. Why this happen? I want to make it circular when I will hidden yes or no.
I have subclassed an UITextField, so that there is an icon next to the text field, here is the code:
iconView = [UIImageView new];
self.textField = [UITextField new];
self.textField.borderStyle = UITextBorderStyleNone;
self.textField.textColor = [UIColor whiteColor];
self.layer.cornerRadius = 5;
self.layer.borderWidth = 1;
self.layer.borderColor = TEXTFIELD_BORDER_COLOR;
self.backgroundColor = [UIColor clearColor];
self.textField.backgroundColor = [UIColor clearColor];
//iconView.backgroundColor = [UIColor clearColor];
[self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
[iconView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:iconView];
[self addSubview:self.textField];
[self setUpConstraints];
When I set an image, which doesn't have any transparency inside, everything works fine, but when I set with transparency I get faint, half visible image.
Here are two examples of image, first displays fine, second is barely visible:
The problem is with your image.
In the first image (the person one), the gray colored area is opaque. And in the second image (the lock), the gray colored area is almost transparent (opacity is at 31%).
Is there a way to overlap 2 or more UIViews with differing background colors and alphas to give the appearance of another color? For example place a red UIView on top of a blue UIView to give the appearance of a single magenta UIView.
On iOS the only blending mode for views if the so-called "source over" mode.
Basically RGB_result = RGB_back * (1 - Alpha_front) + RGB_front * Alpha_front
Thus a red (1, 0, 0) view with 0.5 alpha on top of a blue (0, 0, 1) view will result in dark magenta (0.5, 0, 0.5)
If you need some other blending mode, consider drawing with CoreGraphics (e.g. CGContextSetBlendMode)
You could use the alpha property like so:
UIView *redView = [[UIView alloc] initWithFrame: CGRectMake(0,0,20,20)];
redView.backgroundColor = [UIColor redColor];
UIView *blueView = [[UIView alloc] initWithFrame: CGRectMake(0,0,20,20)];
blueView.backgroundColor = [UIColor blueColor];
blueView.alpha = 0.5;
[redView addSubview: blueView];
Note, this is much easier to achieve in a single view by getting the colour you want manually using the RGB creation method of UIColor:
UIView *magentaView = [[UIView alloc] initWithFrame: CGRectMake(0,0,20,20)];
UIColor *magenta = [UIColor colorWithRed: 1
green: 0
blue: 1
alpha: 1];
magentaView.backgroundColor = magenta;
The RGB values are between 0 and 1, note (not the standard 0 -> 255 range that most would normally be specified with). The alpha value denotes the opacity.
This gives a purple view, no problem.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view1 = [[UIView alloc] initWithFrame:self.view.bounds];
view1.backgroundColor = [UIColor colorWithRed:1.f green:0.f blue:0.f alpha:0.5f];
[self.view addSubview:view1];
view1 = [[UIView alloc] initWithFrame:self.view.bounds];
view1.backgroundColor = [UIColor colorWithRed:0.f green:0.f blue:1.f alpha:0.5f];
[self.view addSubview:view1];
}
The below method working for me
- (void)viewDidLoad {
[super viewDidLoad];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeColor);
}
I have a UITableViewController. In viewDidLoad, I do the following:
self.tableView.backgroundColor = [UIColor clearColor];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg_cafe.png"]];
The background is supposed to be a gradient. Two issues:
Gradient seems to get cut off at the end of the UITableView.
The corners are tinted.
I've experimented with setting the UITableCell's opacity to NO, but that doesn't worked. I have read these threads already: Transparent background in grouped UITableView - iPhone, and Black corners around UITableViewCells.
I'm not using Interface Builder at all for this.
Example:
Make a UIView with that pattern image as background color and set it as table view's background view.
UIView *bgView = [[UIView alloc] initWithFrame:self.tableView.frame];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg_cafe.png"]];
self.tableView.backgroundView = bgView;
[bgView release];