Shadow in UIView without gradient ? - ios

Does any one know how to achieve the shadow effect with no gradient? Like the screenshot show below
Another concern is the sequence of subviews, i.e the view in front may hide the effect of the view in behind. How to overcome this?

For the first problem you can change the shadowRadius of the shadow, for example:
//You must include QuartzCore framework (#import <QuartzCore/QuartzCore.h>)
view.layer.cornerRadius = 5;
view.layer.shadowRadius = 0; //The shadow should be rendered as a solid shape
view.layer.shadowOffset = CGSizeMake(0, 2);
view.layer.shadowOpacity = 0.5;
view.layer.shadowColor = [UIColor blackColor].CGColor;
UIBezierPath *path = [UIBezierPath bezierPathWithRect:view.bounds];
view.layer.shadowPath = path.CGPath; //This is very important!
Remember to always set the shadowPath! If you don't the performance of rendering the shadow will decrease a lot.
For the second problem, sorry but I don't think there's a way to let the shadow of an object appear over another view that is over the original one.

Related

How can I add a drop shadow effect beneath image views on iOS?

I want to add a drop shadow effect underneath my UIImageView instances, like in the screenshot below:
How can I achieve this, either through storyboards or code (Objective-C)?
//Drop Shadow
[view.layer setShadowColor: [UIColor grayColor].CGColor];
[view.layer setShadowOpacity:0.8];
[view.layer setShadowRadius:3.0];
[view.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
Please Try this once :-
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:view.bounds];
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
view.layer.shadowOpacity = 0.5f;
view.layer.shadowPath = shadowPath.CGPath;
Explanation :-
As seen in the last step there are a few different properties needed when adding a shadow using Core Animation. If your are not familiar with Core Animation don’t worry, just try to focus on the shadow code and not the CALayer on the view.
shadowOffset is a CGSize representing how far to offset the shadow from the path.
shadowColor is the color of the shadow. Shadow colors should always be opaque, because the opacity will be set by the shadowOpacity property. The shadowColor property is a CGColor not a UIColor.
shadowRadius is the width of the shadow along the shadow path
shadowOpacity determines the opacity of the shadow.
shadowPath is probably the most important of the properties. While a shadow can be drawn without specifying a path, for performance reasons you should always specify one. This path tells Core Animation what the opaque regions of the view are, and without it, things slow down severely! It is a CGPath, which is most easily created using UIBezierPath (iOS only) as shown in step 2.
See more in http://nscookbook.com/2013/01/ios-programming-recipe-10-adding-a-shadow-to-uiview/
Those are not lights, but shadow. You can use the layer to draw shadow for UI elements.

I can't get shadows to appear around my custom UITableViewCells

cell.layer.shadowOpacity = 1.0;
cell.layer.shadowRadius = 5;
cell.layer.shadowColor = [UIColor blackColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0.0, -2.0);
cell.layer.shadowPath = [UIBezierPath bezierPathWithRect:cell.layer.bounds].CGPath;
I have been trying to get a shadow to appear around my cells, however I haven't had any luck. I've even tried ordering the zPosition of my cells to see if it works, and nothing. What am I missing?
I want to be able to draw my shadows so the cell with index.row = x appears to be above the cell with index.row = x-1
You might get this to work to some extent by setting the layer's masksToBounds to NO. But don't. Don't do any of what you're doing. What you're trying to do is a really bad idea, because you are forcing the render tree to constantly rerender the shadow while the table scrolls, which will cause a low frame rate and stuttering of the scroll.

Shadows slow up UIScrollView

I have a UIScrollView that pretty much functions like a Facebook news feed. I thought my elements were slowing the scroll fps down. By process of elimination, I found out that the shadows slow down my app.
The UIView boxes inside the scroll view have such configuration:
self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowOffset = CGSizeMake(0, 2)
self.layer.shadowRadius = 2
self.layer.shadowOpacity = 0.15
Like a news feed, my scroll view has many boxes, therefore having UIViews with their own shadows. How do I go with this without slowing down my app?
There's a bunch of stuff on speeding up UIScrollViews:
CALayer - Shadow causes a performance hit?
https://markpospesel.wordpress.com/2012/04/03/on-the-importance-of-setting-shadowpath/
If you use custom CALayer instances -- especially with shadows -- stuff that requires processing power, you should use
scrollView.layer.shouldRasterize = YES;
scrollView.layer.rasterizationScale = [UIScreen mainScreen].scale;
Also a shadowPath could speed up your scrollview as well something like this:
[scrollView.layer setShadowPath:[[UIBezierPath bezierPathWithRect:myView.bounds] CGPath]];
Setting a shadowPath allows iOS to not recalculate how it should draw the shadow every time it draws the view, thus improving performance.
In swift, something like this:
let shadowPath = UIBezierPath(rect: view.bounds);
view.layer.masksToBounds = false;
view.layer.shadowColor = UIColor.blackColor().CGColor;
view.layer.shadowOffset = CGSizeMake(0, 0.5);
view.layer.shadowOpacity = 0.2;
view.layer.shadowPath = shadowPath.CGPath;
Setting the shadowPath will improve performance and look the same so long as your views are opaque. You could just set it to the bounds of the view or layer like so:
CGPathRef shadowPath = CGPathCreateWithRect(self.bounds, NULL);
self.layer.shadowPath = shadowPath;
CGPathRelease(shadowPath);
In Swift:
layer.shadowPath = CGPathCreateWithRect(bounds, nil)
The memory management is handled for you (discussed here).

UILabel animation beyond bounds of UILabel

I have a label and I want a "speedometer" effect; when a new value is assigned to the label's text I'd like the old value to scroll up and the new to come in from below. I am agnostic as to how to achieve this. This is what I am using currently;
CATransition *animation = [CATransition animation];
animation.duration = .2f;
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromBottom;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.removedOnCompletion = YES;
[mylabel.layer addAnimation:animation
forKey:#"changeTextTransition"];
mylabel.text = #"new text";
This works pretty dang well EXCEPT that the transition at top and bottom goes outside the bounds of the label before fading. I'd like the transition effect to be contained within the bounds of the label.
I have tried to create a mask for the layer, and have the layer mask to bounds, but this had no effect:
UIImageView *maskView = [[UIImageView alloc] initWithImage:labelMask];
// this image is a png that is the same dimensions as the label, and is 100% opaque and colored white
maskView.frame = mylabel.frame;
mylabel.layer.mask = maskView.layer;
mylabel.layer.masksToBounds = YES;
I even tried an alternate method of mask creation:
CALayer *maskLayer = [CALayer new];
maskLayer.frame = mylabel.frame;
maskLayer.contents = (id)(labelMask.CGImage); // same image as above
maskLayer.contentsRect = mylabel.bounds;
mylabel.layer.mask = maskLayer;
mylabel.layer.masksToBounds = YES;
This had no effect.
What else may I try to prevent the layer animation from leaking past the bounds of the UILabel?
You are moving the UILabel's layer itself with animation, and the UILabel's clipsToBounds (UIView)/masksToBounds (CALayer) is limiting the UILabel's layer itself as a whole.
To make this clearer: If you would put something inside the UILabel, or try to render something bigger on the label's layer, it will be clipped by the mask. But if you just moved the label to somewhere else on the parent view - then it would stay whole, as the mask moves with it...
Even when animating, this is the exact same situation. The mask moves with the layer.
So the solution is containing the UILabel within another UIView, which itself has the clipsToBounds set.
This would also allow you to create a subclass of UIView which is a mini-controller, manages its own labels, animates them etc. on demand with simple interface on the UIView subclass. (I guess that this is what you're doing and you just forgot to set clipsToBounds on the container view...)

Use Bezier Path as Clipping Mask

I am wondering if it is possible to clip a view to a Bezier Path. What I mean is that I want to be able to see the view only in the region within the closed Bezier Path. The reason for this is that I have the outline of an irregular shape, and I want to fill in the shape gradually with a solid color from top to bottom. If I could make it so that a certain view is only visible within the path then I could simply create a UIView of the color I want and then change the y coordinate of its frame as I please, effectively filling in the shape. If anyone has any better ideas for how to implement this that would be greatly appreciated. For the record the filling of the shape will match the y value of the users finger, so it can't be a continuous animation. Thanks.
Update (a very long time later):
I tried your answer, Rob, and it works great except for one thing. My intention was to move the view being masked while the mask remains in the same place on screen. This is so that I can give the impression of the mask being "filled up" by the view. The problem is that with the code I have written based on your answer, when I move the view the mask moves with it. I understand that that is to be expected because all I did was add it as the mask of the view so it stands to reason that it will move if the thing it's tied to moves. I tried adding the mask as a sublayer of the superview so that it stays put, but that had very weird results. Here is my code:
self.test = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
self.test.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.test];
UIBezierPath *myClippingPath = [UIBezierPath bezierPath];
[myClippingPath moveToPoint:CGPointMake(100, 100)];
[myClippingPath addCurveToPoint:CGPointMake(200, 200) controlPoint1:CGPointMake(self.screenWidth, 0) controlPoint2:CGPointMake(self.screenWidth, 50)];
[myClippingPath closePath];
CAShapeLayer *mask = [CAShapeLayer layer];
mask.path = myClippingPath.CGPath;
self.test.layer.mask = mask;
CGRect firstFrame = self.test.frame;
firstFrame.origin.x += 100;
[UIView animateWithDuration:3 animations:^{
self.test.frame = firstFrame;
}];
Thanks for the help already.
You can do this easily by setting your view's layer mask to a CAShapeLayer.
UIBezierPath *myClippingPath = ...
CAShapeLayer *mask = [CAShapeLayer layer];
mask.path = myClippingPath.CGPath;
myView.layer.mask = mask;
You will need to add the QuartzCore framework to your target if you haven't already.
In Swift ...
let yourCarefullyDrawnPath = UIBezierPath( .. blah blah
let maskForYourPath = CAShapeLayer()
maskForYourPath.path = carefullyRoundedBox.CGPath
layer.mask = maskForYourPath
Just an example of Rob's solution, there's a UIWebView sitting as a subview of a UIView called smoothView. smoothView uses bezierPathWithRoundedRect to make a rounded gray background (notice on right). That works fine.
But if smoothView has only normal clip-subviews, you get this:
If you do what Rob says, you get the rounded corners in smoothView and all subviews ...
Great stuff.

Resources