3

Shadow effect

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?

madLokesh
  • 1,860
  • 23
  • 49
Js Lim
  • 3,625
  • 6
  • 42
  • 80

1 Answers1

4

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.

JP Illanes
  • 3,665
  • 39
  • 56
  • It works like a charm. Thanks. Solved the first issue. By the way, why the shadowPath was so important? – Js Lim Jul 09 '13 at 04:15
  • 1
    To be honest I'm not really sure, I think is important because if you don't set it, every time drawRect is called (for example when animating something) in that view, the shadow path needs to be recomputed, but if you set it, it will always use that one. – JP Illanes Jul 09 '13 at 05:06