
我该怎么做呢?我没有找到很多这方面的好教程,也没有谈论在CALayer上渲染NSBezIErPaths.想法有人吗?
解决方法 你的预感是对的,这实际上是优化绘图的一种很好的方法.我自己完成了这个,我有一些大的静态背景,我希望避免在元素移动到顶部时重绘.您需要做的就是为视图中的每个内容项添加CALayer对象.要绘制图层,您应该将视图设置为每个图层的委托,然后实现drawLayer:inContext:方法.
在该方法中,您只需绘制每个图层的内容:
- (voID)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx{ if(layer == yourBackgroundLayer) { //draw your background content in the context //you can either use Quartz drawing directly in the CGContextRef,//or if you want to use the Cocoa drawing objects you can do this: NSGraphicsContext* drawingContext = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:YES]; NSGraphicsContext* prevIoUsContext = [NSGraphicsContext currentContext]; [NSGraphicsContext setCurrentContext:drawingContext]; [NSGraphicsContext saveGraphicsstate]; //draw some stuff with NSBezIErPath etc [NSGraphicsContext restoreGraphicsstate]; [NSGraphicsContext setCurrentContext:prevIoUsContext]; } else if (layer == someOtherLayer) { //draw other layer } //etc etc} 如果要更新其中一个图层的内容,只需调用[yourLayer setNeedsdisplay]即可.然后,这将调用上面的委托方法以提供图层的更新内容.
请注意,默认情况下,当您更改图层内容时,Core Animation会为新内容提供良好的淡入淡出过渡.但是,如果您自己处理绘图,则可能不希望这样,因此为了防止图层内容更改时动画中的默认淡入淡出,您还必须实现actionForLayer:forKey:委托方法并阻止动画通过返回null *** 作:
- (ID<CAAction>)actionForLayer:(CALayer*)layer forKey:(Nsstring*)key { if(layer == someLayer) { //we don't want to animate new content in and out if([key isEqualToString:@"contents"]) { return (ID<CAAction>)[NSNull null]; } } //the default action for everything else return nil;} 总结 以上是内存溢出为你收集整理的cocoa – 我可以使用CALayer来加速视图渲染吗?全部内容,希望文章能够帮你解决cocoa – 我可以使用CALayer来加速视图渲染吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)