Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当图层旋转或缩放时,如何创建与CALayer边界完全匹配的CGPath?_Ios_Calayer_Cgpath - Fatal编程技术网

Ios 当图层旋转或缩放时,如何创建与CALayer边界完全匹配的CGPath?

Ios 当图层旋转或缩放时,如何创建与CALayer边界完全匹配的CGPath?,ios,calayer,cgpath,Ios,Calayer,Cgpath,我们有一个CALayer,它显示带有编辑信息的图像。例如旋转、缩放和平移。我想制作一个与层边界完全匹配的CGPath。当图层旋转时,我的路径也应该旋转,它的四个角应该与CALayer的四个角匹配。CGPath实际上用作灰色遮罩来显示图像的剪裁区域 我尝试下面的代码,但它不起作用 f = self.imageLayer.frame; t = self.imageLayer.affineTransform; CGPathAddRect(path, &t, f); C

我们有一个CALayer,它显示带有编辑信息的图像。例如旋转、缩放和平移。我想制作一个与层边界完全匹配的CGPath。当图层旋转时,我的路径也应该旋转,它的四个角应该与CALayer的四个角匹配。CGPath实际上用作灰色遮罩来显示图像的剪裁区域

我尝试下面的代码,但它不起作用

    f = self.imageLayer.frame;
    t = self.imageLayer.affineTransform;
    CGPathAddRect(path, &t, f);
CALayer有自己的CGAffineTransform。所有编辑信息都通过CGAffineTransform应用


任何提示都将不胜感激,谢谢。

如果我没有弄错您的问题,您可以使用
UIBezierPath
来切割图像层边界,使其余可见屏幕变暗。基本上,您可以创建一个非常大的矩形路径(出于某些原因,CGRectInfinite在这里不起作用)并剪切图像层周围的区域。诀窍是使用
kCAFillRuleEvenOdd
,它可以翻转被认为是内部和外部的内容

像这样的东西应该有用:

    let cutPath = UIBezierPath(roundedRect: imageLayer.bounds, cornerRadius: 0)

    let clipPath = UIBezierPath(rect: CGRectMake(-10e6, -10e6, 10e7, 10e7)) // CGRectInfinite isn't working here ?
    clipPath.appendPath(cutPath)
    clipPath.usesEvenOddFillRule = true

    let shape = CAShapeLayer()
    shape.contentsScale = UIScreen.mainScreen().scale
    shape.lineWidth = 2
    shape.fillColor = UIColor(red:0.1, green:0.1, blue:0.1, alpha:0.5).CGColor
    shape.fillRule = kCAFillRuleEvenOdd
    shape.strokeColor = UIColor.whiteColor().CGColor

    shape.path = clipPath.CGPath
    imageLayer.addSublayer(shape)

    // do a transformations here
    imageLayer.transform = CATransform3DMakeRotation(10.0, 1.0, 1.0, 0.8)
结果是什么


谢谢您的回复。我终于解决了这个问题。我们有一个固定的矩形代表剪辑区域(其大小可能为500*400)。它外面的图像将被剪裁。图像的剪裁部分有一个灰色遮罩。当图像旋转时,遮罩将跟随图像。我使用CGPathAddRect(path,&t,f);和其他现有的CGPathMoveToPoint、CGPathAddLineToPoint代码创建掩码。我们有offsetX和offsetY以及图像层的大小。我围绕(偏移量x,偏移量)点旋转遮罩。我认为你的添加子层和kCAFillRuleEvenOdd技巧对我非常有用。