gpt4 book ai didi

ios - 当 View 从纵向更改为横向时,调整带有透明孔的层的位置

转载 作者:行者123 更新时间:2023-11-28 05:52:09 26 4
gpt4 key购买 nike

我正在使用这段代码创建一个带有透明孔的图层

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)

如果初始 View 是图像中显示的纵向或横向,它工作正常

enter image description here

但是,如果 View 从纵向变为横向或从横向变为纵向

如图所示,层的方向不正确

enter image description here

最佳答案

看起来您需要在用户旋转设备时更新路径框架。您正在寻找类似于 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)
}

关于ios - 当 View 从纵向更改为横向时,调整带有透明孔的层的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52542359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com