【发布时间】:2013-12-22 10:00:27
【问题描述】:
我有一个可拖动的视图,上面有一个遮罩层。遮罩层上有一个 UIBezierPath ,它使视图上的一个区域透明/透明(我想要的效果)。我的最终目标是通过传递基于我的视图和另一个矩形的交集计算的 CGRect 来更改路径的位置和大小(不是遮罩层!)(基本上我想隐藏相交的区域)。
1)我如何创建我的蒙版和路径(在我的视图上创建一个透明矩形):
self.maskLayer = [CAShapeLayer layer];
self.maskLayer.frame = self.contentView.bounds;
//default path rect
CGRect const rect = CGRectMake(CGRectGetMidX(self.contentView.bounds) ,
CGRectGetMidY(self.contentView.bounds),
50,50);
path = [UIBezierPath bezierPathWithRect:rect];
[path appendPath:[UIBezierPath bezierPathWithRect:self.contentView.frame]];
self.maskLayer.path = path.CGPath;
self.maskLayer.fillRule = kCAFillRuleEvenOdd;
self.contentView.layer.mask = self.maskLayer;
2) 我的问题是我找不到仅调整路径大小/重新定位的方法。我在网上搜索,但没有找到任何解决方案。
此代码不起作用,但这是我能想到的:
//runs while the view is being dragged(using UIPanGestureRecognizer)
-(void)move{
CGRect intersectedRect = CGRectIntersection(self.frame, self.productView.frame);
path = [UIBezierPath bezierPathWithRect:intersectedRect];
[path appendPath:[UIBezierPath bezierPathWithRect:intersectedRect]];
[self.maskLayer setNeedsDisplay];
}
如何在我的移动功能中仅操作遮罩层的路径? 谢谢!
【问题讨论】:
-
第二个sn-p中的path变量是从哪里得到的?你在哪里设置蒙版层的路径?为什么要将相同的矩形附加到路径?
-
我使用第一个 sn-p 中的相同路径变量,一个全局路径变量。我在第一个 sn-p 上设置了路径(self.maskLayer.path = path.CGPath;)我将 self.contentView.frame 附加到路径,抱歉是类型错误。
-
是的,但我看不到您在 move 方法中分配更新路径的位置。
-
-(void)move{ CGRect intersectedRect = CGRectIntersection(self.frame, self.productView.frame);路径 = [UIBezierPath bezierPathWithRect:intersectedRect]; [路径 appendPath:[UIBezierPath bezierPathWithRect:self.contentView.frame]]; self.maskLayer.path = path.CGPath; self.maskLayer.fillRule = kCAFillRuleEvenOdd; self.contentView.layer.mask = self.maskLayer; }
-
现在它正在更新,但在 10 秒后。你知道为什么会这样吗?
标签: ios iphone ios7 mask uibezierpath