I am trying to draw shadows in a rectangle. The shadow image itself is 1px * 26px.
Here are two methods that I thought of for drawing an image in a view:
[self.upperShadow drawInRect:rectHigh];
[self.lowerShadow drawInRect:rectLow];
CALayer *shadowTop = [CALayer layer];
shadowTop.frame = rectHigh;
shadowTop.contents = (__bridge id)topShadow;
[self.layer addSublayer:shadowTop];
CALayer *shadowLow = [CALayer layer];
shadowLow.frame = rectLow;
shadowLow.contents = (__bridge id)lowShadow;
[self.layer addSublayer:shadowLow];
UIImageView *tShadow = [[UIImageView alloc] initWithFrame:rectHigh];
UIImageView *bShadow = [[UIImageView alloc] initWithFrame:rectLow];
tShadow.image = self.upperShadow;
bShadow.image = self.lowerShadow;
tShadow.contentMode = UIViewContentModeScaleToFill;
bShadow.contentMode = UIViewContentModeScaleToFill;
[self addSubview:tShadow];
[self addSubview:bShadow];
I'm curious which one is better when it comes to performance in drawing and animation. From my benchmarking, it seems that layers are faster to draw. Here are some comparatives:
drawInRect: took 0.00054 secs
CALayers took 0.00006 secs
UIImageView took 0.00017 secs
In the view that contains these shadows, it will be displayed above it, which will be animated (the view itself is not). Avoid that would degrade the performance of the animation. Any thoughts between the three methods?