简体   繁体   English

CALayer掩码在设备旋转时不会更新

[英]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 
  }
  1. You should subclass your view, as it's not MVC approach to implement layers in the controller. 您应该继承视图的子类,因为它不是在控制器中实现层的MVC方法。
  2. You you call layoutSublayers you should also call super . 您调用layoutSublayers也应该调用super
  3. When you subclass your view and implement the layer there, keep a reference to it and in method 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM