[英]CALayer mask does update on device rotation
I have a CAShapeLayer
subclass that creates a hexagon as a path from it's bounds. 我有一个
CAShapeLayer
子类,该子类创建一个六边形作为从其边界的路径。 I have another CALayer
and add an image as the CALayer's content
and then I set the CALayer's mask
property to the hexagon layer. 我有另一个
CALayer
,并将图像添加为CALayer的content
,然后将CALayer的mask
属性设置为六边形层。 When I add this as a sublayer of my viewController's view the image displays correctly (clipped as a hexagon). 当我将其添加为viewController视图的子层时,图像会正确显示(剪切为六边形)。
-(void)viewDidLoad {
[super viewDidLoad];
MBFHexLayer *hex = [MBFHexLayer hexLayer];
hex.orientation = MBFHexOrientationPointyTopped;
hex.frame = self.view.bounds;
_hexTile = [CALayer layer];
_hexTile.frame = self.view.bounds;
_hexTile.mask = hex;
_hexTile.contents = (id)[UIImage imageNamed:@"grass.jpg"].CGImage;
[self.view.layer addSublayer:_hexTile];
}
In the viewDidLayoutSubviews
I set the bounds of the image layer to the bounds of the view and I have overridden layoutSublayers
in my hexagon mask layer to recompute the path. 在
viewDidLayoutSubviews
,将图像层的边界设置为视图的边界,并且在六边形蒙版层中重写了layoutSublayers
以重新计算路径。
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"View Bounds:%@", NSStringFromCGRect(self.view.bounds));
_hexTile.frame = self.view.bounds;
[_hexTile setNeedsLayout];
[_hexTile layoutSublayers];
}
CAShapeLayer: CAShapeLayer:
-(void)layoutSublayers {
[self recalcGeometry];
self.path = [self layerPath].CGPath;
}
When I rotate the display the layoutSublayers
method is not called on my hexagon mask layer and the image is no longer clipped correctly in the view. 旋转显示器时,六边形蒙版层上未调用
layoutSublayers
方法,并且视图中的图像不再正确剪切。
I am confused as to what the correct update chain is and any pointers would be greatly appreciated. 我对正确的更新链是什么感到困惑,任何指针将不胜感激。
I would implement this Method: 我将实现此方法:
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation {
//relayout
}
MVC
approach to implement layers in the controller. MVC
方法。 layoutSublayers
you should also call super
. layoutSublayers
也应该调用super
。 layoutSublayersOfLayer:
you should call setFrame:
layoutSublayersOfLayer:
方法中layoutSublayersOfLayer:
您应调用setFrame:
You may check similar approach here: https://github.com/natalia-osa/NORosettaView/blob/master/NORosettaView/Classes/Example2View.m 您可以在此处检查类似的方法: https : //github.com/natalia-osa/NORosettaView/blob/master/NORosettaView/Classes/Example2View.m
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.