gpt4 book ai didi

iphone - 第一次更改 View 的 layer.mask 后不会更新

转载 作者:行者123 更新时间:2023-11-29 13:16:51 25 4
gpt4 key购买 nike

我需要一个 View 在某些条件下有圆角,在其他条件下有规则的方形。

但是,当我在第一次设置 View 后尝试重新设置 View 的 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;
}

但是没有视觉上的变化。我需要做什么?

最佳答案

您应该避免修改布局 subview 中的 View (或在本例中为图层)层次结构。我建议创建一个像 - (void)setRoundedCornersEnabled:(BOOL)roundedCornersEnabled 这样的方法来适本地修改图层以创建圆形或方形效果。

在大多数情况下,您在layoutSubviews 中唯一需要的代码是修改 subview 框架的代码,无论什么layoutSubviews 都应该在您的 View 的所有可能状态下工作(在这种情况下启用或不启用圆角)。 UIKit 经常自行调用 layoutSubviews,因此您不能完全控制何时调用该方法。

关于iphone - 第一次更改 View 的 layer.mask 后不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com