我用以下代码使用UIBezierPath和CAShapeLayer制作掩码:
- (void)createPath {
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(100, 100)];
[path moveToPoint:CGPointMake(100, 100)];
[path addLineToPoint:CGPointMake(0, 100)];
[path moveToPoint:CGPointMake(0, 100)];
[path addLineToPoint:CGPointMake(0, 0)];
[path closePath];
CAShapeLayer *layer = [CAShapeLayer new];
layer.frame = self.contentView.bounds;
layer.path = path.CGPath;
self.contentView.layer.mask = layer;
}但我的contentView并没有被掩盖,而是完全消失了。我试着在调试器中查看path,它看起来与我希望它看到的完全一样。
发布于 2018-09-30 15:15:42
使用layer.mask时,首先要找到正确的路径。你不需要每次都搬到一个新的地方。通过这种方式,您的路径由三或四个子路径组成,这些子路径不能被关闭以形成正确的路径。
第二种方法是尝试在View类本身中使用,而不是调用其他子视图,比如contentView。因为您可能不知道什么时候在子视图中调用它。在UIView子类中运行以下命令,比如在UITableViewCell中(从nib唤醒)。你能明白我的意思。如果您真的想使用contentView,只需找到正确的位置放置您的层代码。比如覆盖setNeedLayout等等。
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self createPath];
}
- (void)createPath {
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(100, 100)];
// [path moveToPoint:CGPointMake(100, 100)];
[path addLineToPoint:CGPointMake(0, 100)];
// [path moveToPoint:CGPointMake(0, 100)];
[path addLineToPoint:CGPointMake(0, 0)];
[path closePath];
CAShapeLayer *layer = [CAShapeLayer new];
layer.frame = self.contentView.bounds;
layer.path = path.CGPath;
self.layer.mask = layer; // not self.contentView.layer.mask;
}https://stackoverflow.com/questions/52574133
复制相似问题