Overlapping UIViews of Differing Color and Alpha - ios

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);
}

Related

bringSubviewToFront does not work on custom button's subview [duplicate]

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)

Add image circle in UIImageView using objective c

I have a UIImageView and this is my code to show image :
callRecImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"call_accept.png"]];
[callRecImage setFrame:CGRectMake(133,top, 36, 40)];
[self.view addSubview: callRecImage];
To add circle around image I did this :
callRecImage.layer.cornerRadius = callRecImage.frame.size.width /2; callRecImage.layer.borderWidth = 2.0f;
callRecImage.layer.borderColor = [UIColor grayColor].CGColor; callRecImage.clipsToBounds = YES;
my image view is :
my output is :
I want to add a circle to my UIImageView, I have followed this link image in circle frame iOS but it's working as I expected
I want a big circle around the phone image. any kind of help will be appreciated.
Thanks in advance
[callRecImage setFrame:CGRectMake(133,top, 36, 40)];
//Set Height and width equal
//Like
[callRecImage setFrame:CGRectMake(133,top, 40, 40)];
use the following code
#import <QuartzCore/QuartzCore.h>
callRecImage.layer.masksToBounds = YES;
callRecImage.layer.cornerRadius = callRecImage.bounds.size.width/2;
callRecImage.layer.borderWidth = 2.0f;
callRecImage.layer.borderColor = [UIColor grayColor].CGColor;
Add the below line to fill you image in ImageView only
callRecImage.contentMode = UIViewContentModeScaleAspectFill;
Try this code
callRecImage.layer.backgroundColor=[[UIColor clearColor] CGColor];
callRecImage.layer.cornerRadius=20;
callRecImage.layer.borderWidth=2.0;
callRecImage.layer.masksToBounds = YES;
callRecImage.layer.borderColor=[[UIColor grayColor] CGColor]
Whenever you go for layer.cornerRadius for the same view then circle will be always smaller then the view rectangle so use one of these two solution.
I have tested and both the solutions are in working condition
Use following code to change UIViewContentMode of your image and increase your UIImageView frame size (height and width, both should be in equal)
callRecImage.contentMode = UIViewContentModeCenter;
So that if you increase the UIImageView frame size, It will keep your image in center and won't stretch your image inside UIImageView
UIImageView *callRecImage = [[UIImageView alloc] initWithFrame:CGRectMake(133, top, 60, 60)];
callRecImage.contentMode = UIViewContentModeCenter;
[callRecImage setImage:[UIImage imageNamed:#"call_accept.png"]];
callRecImage.layer.cornerRadius = callRecImage.frame.size.width /2;
callRecImage.layer.borderWidth = 2.0f;
callRecImage.layer.borderColor = [UIColor grayColor].CGColor;
callRecImage.clipsToBounds = YES;
[self.view addSubview: callRecImage];
or
Add one UIView as superview of your UIImageView and add layer.cornerRadius to that UIView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(133, top, 60, 60)];
UIImageView *callRecImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"call_accept.png"]];
[callRecImage setFrame:CGRectMake(view.frame.size.width/2 - 36/2, view.frame.size.height/2 - 40/2, 36, 40)];
view.layer.cornerRadius = view.frame.size.width /2;
view.layer.borderWidth = 2.0f;
view.layer.borderColor = [UIColor grayColor].CGColor;
view.clipsToBounds = YES;
[view addSubview: callRecImage];
[self.view addSubview: view];

How do I (successfully) set a maskView to a UIView?

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];

How do I crop a MKMapView (or any UIView) into a custom shape?

I'm trying to size an MKMapView in the following way:
Elements should be able to appear behind the arc at the top, so it appears that the view is not square. I know this should be possible with CALayer, but perhaps someone has done it before?
This should be quite possible if you put your MKMapView within a UIView, then apply a mask to that.
Here's a very (!) basic example:
All I've done is put my map in a UIView called "mapContainer" and added a mask to it using this:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect rect = CGRectInset(self.view.frame, 70, 70);
UIView* newView = [[UIView alloc] initWithFrame:rect];
newView.layer.cornerRadius = 55.0;
newView.backgroundColor = [UIColor redColor];
self.mapContainer.layer.mask = newView.layer;
}
And you could add a border to it...
..with a few more lines...
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect rect = CGRectInset(self.view.frame, 70, 70);
UIView* newView = [[UIView alloc] initWithFrame:rect];
newView.layer.cornerRadius = 55.0;
newView.backgroundColor = [UIColor redColor];
UIView* borderView = [[UIView alloc] initWithFrame:rect];
borderView.backgroundColor = [UIColor clearColor];
borderView.layer.cornerRadius = 55.0;
borderView.layer.borderWidth = 3.0;
borderView.layer.borderColor = [[UIColor redColor] CGColor];
[self.mapContainer addSubview:borderView];
[self.mapContainer bringSubviewToFront:borderView];
self.mapContainer.layer.mask = newView.layer;
}
I hope this points you in the right direction.
Unlike Apple Maps.
;-)

How can I build a transparent view with shadow outside in iOS?

I want to build a totally transparent view, the backgroundColor maybe clearColor. Inside this view I want to put a small image. And in the four sides of this transparent view, I want to see the effect of shadow. The shadow must be totally outside the view. I know that I must override the drawRect method in UIView, but don't know how to do this.
try this....
UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(50.0, 100.0, 100.0, 100.0)];
shadowView.layer.cornerRadius = 15.0;
CALayer *layer = shadowView.layer;
layer.shadowOpacity = 1.0;
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowOffset = CGSizeMake(0,0);
layer.shadowRadius = 15;
[self.view addSubview:shadowView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)];
[imageView setImage:[UIImage imageNamed:#"icon.png"]];
[shadowView addSubview:imageView];
What is the problem with doing something like this?
#import <QuartzCore/QuartzCore.h> // at the top of the file
UIView *transparentView = [[UIView alloc] initWithFrame:CGRectMake(50,50,200,200)];
transparentView.backgroundColor = [UIColor greenColor];
transparentView.alpha = 0.5f;
transparentView.clipsToBounds = NO;
transparentView.layer.shadowColor = [UIColor blackColor].CGColor;
transparentView.layer.shadowOpacity = 1;
transparentView.layer.shadowRadius = 1;
UIImageView *imageView = [[]UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
imageView.image = [UIImage imageNamed:#"IMAGE NAME"];
[transparentView addSubview:imageView];
As you can see, based on your description, no drawRect: required, unless for some reason you want to draw the shadow manually, but there isn't a reason to.

Resources