简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM