Question

I'm wondering if there is a way to bring layer on top of others layers of view. Something like bringSubviewToFront does for UIView class. I think it can be done with zPosition property of the CALayer but this means I have to check zPosition for all layers and then set proper value.

Thanks in advance.

Was it helpful?

Solution

I believe that the code (given that layer is your CALayer)

[layer retain];
CALayer *superlayer = layer.superlayer;
[layer removeFromSuperlayer];
[superlayer addLayer:layer];
[layer release];

will do what you want, albeit in a roundabout way.

OTHER TIPS

It's even easier. If sublayerToBeMovedToFront is the layer you're moving and itsSuperLayer is, well, its superlayer, just say:

[itsSuperLayer addSublayer:sublayerToBeMovedToFront];

The addSublayercall simultaneously unhookssublayerToBeMovedToFront from wherever it was in the sibling list and rehooks it as the last (ie, frontmost-positioned, visually "on top") sublayer. This is exactly analogous behaviour to that of [aUIView addSubview:]

This category will achieve what you are talking about…

CALayer+Additions.h

#import <Foundation/Foundation.h>
#import <QuartzCore/CALayer.h>
@interface CALayer (Additions)
- (void)moveToFront;
@end

CALayer+Additions.m

#import "CALayer+Additions.h"
@implementation CALayer (Additions)
- (void)moveToFront {
    CALayer *superlayer = self.superlayer;
    [self removeFromSuperlayer];
    [superlayer addSublayer:self];
}
@end

#import "CALayer+Additions.h" within your project, and you will find a new method, moveToFront is now available to your CALayer instances.

If you're using CALayer for performance you should also try using only UIImageView's. In my case it turned out to be a few FPS faster. (maybe from the UIImage to CGImage conversion that I needed to perform in every image attribution)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top