0

as seen in this question, one can add drop down shadows by using this type of code:

self.layer.shadowOffset = CGSizeMake(0, 1);
self.layer.shadowRadius = 1;
self.layer.shadowOpacity = 0.3;

But this cannot be hardware accelerated, so it takes long to render and when used in a tableview, that needs to redraw every cell, it slows down a lot!

Having this settled, does anyone know a way for doing this smoothly?

Community
  • 1
  • 1
Felipe Jun
  • 722
  • 11
  • 22

1 Answers1

1

I found a solution for my problem, it is more like a workaround, but is working fine for me, I created the custom class ShadowVIew, that does the proper setup for any UIView and adds a dark transparent view below it to look like a shadow:

here's the code:

ShadowView.m

#import "ShadowView.h"

@interface ShadowView()

@property (nonatomic) UIView * shadow;

@end

@implementation ShadowView

-(id)initWithCoder:(NSCoder *)aDecoder{

    if ((self = [super initWithCoder:aDecoder])) {
        self.layer.masksToBounds = NO;
        self.layer.cornerRadius = 8; // if you like rounded corners
    }
    return self;

}

-(void)layoutSubviews{
    [super layoutSubviews];

    if (self.shadow.superview == nil) {
        UIView * parent = self.superview;
        self.shadow = [[UIView alloc] initWithFrame:self.frame];
        self.shadow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        CGRect frame = self.shadow.frame;
        frame.origin.y+=1;
        frame.origin.x-=1;
        frame.size.width+=2;
        self.shadow.frame = frame;
        self.shadow.layer.masksToBounds = NO;
        self.shadow.layer.cornerRadius = 8;

        [parent insertSubview:self.shadow atIndex:0];
        }


@end

Remember, this is not the perfect solution, but works fine for a fast implementation. You can simply add the shadows drawn by quartz if your view is not loaded often, so it will not affect the performance of the app.

self.layer.shadowOffset = CGSizeMake(0, 1);
self.layer.shadowRadius = 1;
self.layer.shadowOpacity = 0.3;

Hope it helps somebody :)

Felipe Jun
  • 722
  • 11
  • 22