dan*_*web 3 iphone cocoa-touch core-animation calayer
我只是改写了我的游戏以使用核心动画来努力使其更快.我有一个UIView,其中包含许多子图层(游戏"图块").tile子层实际上是CALayer子类,它们使用绘制自己的内容drawInContext:
.例如,当选择一个磁贴时,需要重新绘制它,所以我调用[tileLayer setNeedsDisplay]
并得到一堆无效的上下文错误.如果我打电话[mainView setNeedsDisplay]
或[mainView.layer setNeedsDisplay]
,没有任何瓷砖重绘.如何才能重新绘制单个图块(CALayer子类)?
编辑:从非常有用的评论,我能够弄清楚这只发生在CALayer子类绘制图像时.以下是错误:
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetBlendMode: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
Run Code Online (Sandbox Code Playgroud)
以下是导致错误的代码行:
[[UIImage imageNamed:@"flag.png"] drawInRect:self.bounds];
Run Code Online (Sandbox Code Playgroud)
我还有另一种方法是在CALayer子类中绘制图像吗?
小智 5
Core Animation不了解UIKit,即drawInContext:
没有将CALayer上下文设置为UIKit的当前上下文.drawInRect:
UIImage中的方法在当前上下文中绘制图像.要手动将上下文推送到UIKit,请在完成后使用UIGraphicsPushContext()
并弹出它UIGraphicsPopContext()
.
例如:
UIGraphicsPushContext(context);
[[UIImage imageNamed:@"flag.png"] drawInRect:self.bounds];
UIGraphicsPopContext();
Run Code Online (Sandbox Code Playgroud)