I have a CAShapeLayer
subclass that creates a hexagon as a path from it's bounds. 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. When I add this as a sublayer of my viewController's view the image displays correctly (clipped as a hexagon).
-(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.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"View Bounds:%@", NSStringFromCGRect(self.view.bounds));
_hexTile.frame = self.view.bounds;
[_hexTile setNeedsLayout];
[_hexTile layoutSublayers];
}
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.
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. layoutSublayers
you should also call super
. layoutSublayersOfLayer:
you should call setFrame:
You may check similar approach here: https://github.com/natalia-osa/NORosettaView/blob/master/NORosettaView/Classes/Example2View.m
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.