luca
luca

Reputation: 37136

IOS - Quartzcore custom UIView with shadow

I'm implementing a custom UIView to display inside a UIScrollview. The thing is that I need the view to drop a shadow so I did:

#import <QuartzCore/QuartzCore.h>
@implementation CustomView

-(void)setupView{


  self.layer.shadowColor = [UIColor blackColor].CGColor;
  self.layer.shadowOpacity = 0.5;
  self.layer.shadowRadius = 1;
  self.layer.shadowOffset = CGSizeMake(.6f, .6f);
  self.layer.cornerRadius = 2;

  [...]

}

-(id)initWithFrame:(CGRect)frame{
  if((self = [super initWithFrame:frame])){
    [self setupView];
  }

  return self;
}

[...]

The point is that when I build and run this the scrollview is so slow and I just need to remove those lines where I was hacking the "self.layer" and The scrollview goes fast and smooth again.

whats the proper way to add shadows to my custom View?

Upvotes: 1

Views: 871

Answers (1)

rckoenes
rckoenes

Reputation: 69469

This has to do with the all the redrawing that the UIView has to do when moving.

If you rasterize the layer it will become way smoother, this should do the trick:

self.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.layer.shouldRasterize = YES;

You could try to add a shadow path:

self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds];

This might help a bit more.

Upvotes: 2

Related Questions