如果我的问题得到解决,您可以使用UIBezierPath
的usesEvenOddFillRule
来削减图像层边界,使可见屏幕的其余部分变暗。基本上你会创建一个非常大的矩形路径(出于某些原因,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(路径,&t, f);和其他现有的CGPathMoveToPoint,CGPathAddLineToPoint代码来创建掩码),我们有offsetX和offsetY以及图像层的大小,并且我围绕旋转蒙版(offsetX,offsetY)指向我认为你的add subLayer和kCAFillRuleEvenOdd技巧对我来说非常有用。 –