我使用这段代码来创建一个带有透明孔的图层。
let radius = min(self.view.frame.size.width,self.view.frame.size.height)
print(radius)
let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), cornerRadius: 0)
let circlePath = UIBezierPath(roundedRect: CGRect(x: 0, y: self.view.frame.size.height/2 - radius/2 , width: radius, height: radius), cornerRadius: radius/2)
path.append(circlePath)
path.usesEvenOddFillRule = true
fillLayer.path = path.cgPath
fillLayer.fillRule = kCAFillRuleEvenOdd
fillLayer.fillColor = UIColor.black.cgColor
fillLayer.opacity = 0.5
view.layer.addSublayer(fillLayer)如果最初的视图是人像或景观(如图像中所示),则效果很好。

然而,如果视图从人像到景观或景观变化到肖像。
如图所示,该层没有正确地定向。

发布于 2018-09-27 17:58:23
看起来,每当用户旋转他们的设备时,您都需要更新路径的框架。你在寻找类似于here的答案。
如果您需要在过渡过程中进行动画处理,请尝试如下所示:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { context in
// update frames here
}, completion: nil)
}https://stackoverflow.com/questions/52542359
复制相似问题