IOS: sharp-edged shadow

I am trying to add some shadows to one of my representations, and what I would like to achieve is to draw shadows on one side only and let them have sharp edges. No. I tried several methods with no luck (using the shadow-related properties of CALayer + UIBezierPaths). However, iOS always creates a shadow with soft edges:

enter image description here

But what I really want to achieve is something like this (without round corners and sharp edges on the sides , except for one ):

enter image description here

Is there any elegant way to do this, or will I have to draw the shadow myself using CoreGraphics?

PS: I forgot to mention, my view should be a normal UIButton, so overriding drawRect: it will be painful here

+3
source share
2 answers

I experienced a mask that removes shadow from the screen ... so maybe you can try this.

    CALayer *mask = [[[CALayer alloc] init] autorelease];
    mask.backgroundColor = [UIColor blackColor].CGColor;
    mask.frame = CGRectMake(0.0, 0.0, yellowView.bounds.size.width + shadowWidth, yellowView.bounds.size.height);
    yellowView.layer.mask = mask;
+4
source

I think you want to change this value to shadowRadius - set this value to zero and you should get the sharp edge you are looking for:

myView.layer.shadowRadius = 0;
+2
source

All Articles