BezierPath和掩蔽 [英] BezierPath and masking

查看:113
本文介绍了BezierPath和掩蔽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的视图中设置UIBezierPath作为掩码:
目标是这样的:

I want to set up UIBezierPath as mask into my view: The goal is something like this:

所以我用框架绘制视图:

So i draw the View with frame:

CGFloat round = 80;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width/2-105, 0, 210, 60+round)];
myView.backgroundColor = [UIColor redColor];

然后尝试添加BezierMask:

And then try to add BezierMask:

UIBezierPath *aPath = [UIBezierPath bezierPath];

CGSize viewSize = CGSizeMake(myView.frame.size.width, myView.frame.size.height); //(210,80)
CGPoint startPoint = CGPointMake(myView.frame.origin.x,myView.frame.origin.y); //(279,0)

[aPath moveToPoint:startPoint]; //(279,0)

[aPath addLineToPoint:CGPointMake(startPoint.x+viewSize.width,startPoint.y)]; //(489,0)
[aPath addLineToPoint:CGPointMake(startPoint.x+viewSize.width,startPoint.y+viewSize.height-round)]; //(489,60)
[aPath addQuadCurveToPoint:CGPointMake(startPoint.x,startPoint.y+viewSize.height-round) controlPoint:CGPointMake(startPoint.x+(viewSize.width/2),80)]; //(279,60) : (384,80)

[aPath closePath];

CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = myView.bounds;
layer.path = aPath.CGPath;
myView.layer.mask = layer;

[self addSubview:myView];

但是之后我的观点正在消失。为什么?

But my view is disappearing after that. Why?

推荐答案

我猜你正在为掩码层使用不正确的协调。您的遮罩层必须位于遮挡的视图中,而不是在视图的超级视图中。

I guess you are using improper coordinations for your mask layer. Your mask layer must be positioned within the view which it masks, not in the view's superview.

尝试以下更改:

CGSize viewSize = myView.bounds.size;
CGPoint startPoint = CGPointZero;

你的代码经过那些小改动对我来说没问题。

Your code with those little modifications works OK for me.

这篇关于BezierPath和掩蔽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆