iOS Core Animation: CALayer bringSublayerToFront?

前端 未结 8 1731
情深已故
情深已故 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 13:03

    Create a category of CALayer like this:

    @interface CALayer (Utils)
    
    - (void)bringSublayerToFront;
    
    @end
    
    @implementation CALayer (Utils)
    
    - (void)bringSublayerToFront {
        CGFloat maxZPosition = 0;  // The higher the value, the closer it is to the front. By default is 0.
        for (CALayer *layer in self.superlayer.sublayers) {
            maxZPosition = (layer.zPosition > maxZPosition) ? layer.zPosition : maxZPosition;
        }
        self.zPosition = maxZPosition + 1;
    }
    
    @end
    

提交回复
热议问题