iOS Core Animation: CALayer bringSublayerToFront?

前端 未结 8 1737
情深已故
情深已故 2020-12-25 12:28

How do I bring a CALayer sublayer to the front of all sublayers, analogous to -[UIView bringSubviewToFront]?

8条回答
  •  鱼传尺愫
    2020-12-25 12:54

    This is variation of @MattDiPasquale's implementation which reflects UIView's logic more precisely:

    - (void) bringSublayerToFront:(CALayer *)layer
    {
        [layer removeFromSuperlayer];
        [self insertSublayer:layer atIndex:[self.sublayers count]];
    }
    
    - (void) sendSublayerToBack:(CALayer *)layer
    {
        [layer removeFromSuperlayer];
        [self insertSublayer:layer atIndex:0];
    }
    

    Note: if you don't use ARC, you may wish to add [layer retain] at top and [layer release] at bottom of both functions to make sure layer is not accidentally destructed in a case it has retain count = 1.

提交回复
热议问题