ios-如何在具有定位的上下文中呈现CALayer
我必须将ios-如何在具有定位的上下文中呈现CALayer,ios,swift,calayer,cgcontext,Ios,Swift,Calayer,Cgcontext,我必须将CALayer放在CGContext 因此,我在类CALayer:)中找到了一个函数render(在:CGContext中)) 但这对我来说还不够:( 因为此函数无法在CGContext 所以我尝试设置CALayer的position,但它没有像我预期的那样工作 有什么办法定位它吗 这是我的密码 ... let context: CGContext = UIGraphicsGetCurrentContext() let myLayer: CALayer = ... myLayer.p
CALayer
放在CGContext
因此,我在类CALayer
:)中找到了一个函数render(在:CGContext中)
)
但这对我来说还不够:(
因为此函数无法在CGContext
所以我尝试设置CALayer
的position
,但它没有像我预期的那样工作
有什么办法定位它吗
这是我的密码
...
let context: CGContext = UIGraphicsGetCurrentContext()
let myLayer: CALayer = ...
myLayer.position = CGPoint(x: 100, y: 100) // It didn't work as I expected
myLayer.render(in: context)
适用于仍在寻找此问题解决方案的人员。您可以在以下上下文中使用:
if let context = UIGraphicsGetCurrentContext() {
context.translateBy(x: 100.0, y: 100.0)
myLayer.render(in: context)
}
您可以尝试使用
cgcontexttranslatecm
链接,确保您的视图配置为使用自定义图层。请特别查看“配置视图的视觉外观”谢谢!我从你的评论中得到了一些提示,并找到了CGContext
的translateBy
。当我使用它时,它可以正常工作。@Byoth你能分享你的工作代码吗?