[英]changing the layer.mask of a view doesn't update after the first time
我需要在某些条件下具有圆角,而在其他条件下则具有规则的正方形。
但是,当我尝试在第一次设置视图后重新设置其layer.mask时,在视觉上没有任何变化。
我首先在layoutSubviews方法中设置角点
- (void)layoutSubviews
{
// Create the mask image by calling the function
UIImage *mask = MTDContextCreateRoundedMask( self.bounds, 10.0, 0.0, 10.0, 0.0 );
// Create a new layer that will work as a mask
CALayer *layerMask = [CALayer layer];
layerMask.frame = self.bounds;
// Put the mask image as content of the layer
layerMask.contents = (id)mask.CGImage;
// set the mask layer as mask of the view layer
self.layer.mask = layerMask;
}
然后一旦满足条件,我就调用另一个方法
- (void)resetCorners {
// Create the mask image by calling the function
UIImage *mask = MTDContextCreateRoundedMask( self.bounds, 0.0, 0.0, 0.0, 0.0 );
// Create a new layer that will work as a mask
CALayer *layerMask = [CALayer layer];
layerMask.frame = self.bounds;
// Put the mask image as content of the layer
layerMask.contents = (id)mask.CGImage;
// set the mask layer as mask of the view layer
self.layer.mask = layerMask;
}
但是没有视觉变化。 我需要做什么?
您应该避免在layoutSubviews中修改视图(或本例中的图层)层次结构。 我建议创建类似- (void)setRoundedCornersEnabled:(BOOL)roundedCornersEnabled
,该方法会适当修改图层以创建圆形或方形效果。
在大多数情况下,您只需要在layoutSubviews
中使用的代码是修改子视图的框架的代码,并且无论哪种layoutSubviews
在视图的所有可能状态下都可以工作(在这种情况下,是否启用了圆角)。 UIKit经常自行调用layoutSubviews,因此您不能依赖完全控制何时调用该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.