我有一个可拖动的视图,上面有一个遮罩层.掩码层上有一个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;
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
如何在移动函数中操纵遮罩层的路径?谢谢!