Move the CALayer frame, but not the clip mask.

I need to move a layer by setting its frame, but without moving the clip of the layer.

I created a layer that is created as follows:

layer = [CustomLayer layer]; layer.frame = CGRectMake(50, 50, 100, 30); [layer setNeedsDisplay]; [self.view.layer addSublayer:layer]; 

And a clip layer, for example:

 clipLayer = [CAShapeLayer layer]; UIBezierPath *clipPath = [UIBezierPath bezierPathWithRoundedRect:layer.bounds cornerRadius:10.0f]; clipLayer.path = clipPath.CGPath; layer.mask = clipLayer; 

However, at some later point in my code, if I set the layer frame:

 layer.frame = CGRectOffset(layer.frame, -20.0, 0.0); 

Both layers and its clip mask are offset. The effect I'm trying to achieve is to “scroll” a layer under a rounded rectangular medium. Any ideas?

+4
source share
1 answer

I would pin the clip layer to the third level. This third layer should:

  • be the same layer size you want to mask
  • be in the same position of the layer you want to mask
  • color clearColor

then just move the underlying layer, and the third layer with its trimmed layer will remain in place.

NOTE. This worked for me - although you also need to make sure that the layer you are moving is a subordinate clipping layer.

+4
source

Source: https://habr.com/ru/post/1469088/


All Articles