如何使用CALayers保存状态? [英] How do I save state with CALayers?

查看:138
本文介绍了如何使用CALayers保存状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的iphone应用程序,使用CALayer的子类绘制块,并试图找到最好的方式来保存状态或持久化当前创建的图层。



我尝试使用 Brad Larson对此问题的回答存储NSUserDefaults中的自定义对象,它用于保存我的CALayer子类,但不是像几何体和背景颜色的基本状态,所以他们在那里,但没有在重新启动时呈现。



我使我声明的实例变量符合NSCoding协议,但不知道如何使CALayer的属性相同,而不重新声明它的所有属性。或者这不是正确的/最好的方法吗?



这是我用来存档层数组的代码:

  [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:viewController.view.layer.sublayers] forKey:@savedArray]; 

这里是我用来重载我的图层在-viewDidLoad代码:

  NSUserDefaults * currentDefaults = [NSUserDefaults standardUserDefaults]; 

NSData * dataRepresentingSavedArray = [currentDefaults objectForKey:@savedArray];

if(dataRepresentingSavedArray!= nil){

[self restoreStateWithData:dataRepresentingSavedArray];

并在-restoreStateWithData中:

  NSArray * savedLayers = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 

if(savedLayers!= nil){

for(layer in savedLayers){

[self.view.layer addSublayer:layer];

}

[spaceView.layer layoutSublayers]; Cocoa(和Cocoa Touch)可以通过使用Cocoa和Cocoa Touch来实现这一点。主要基于模型 - 视图 - 控制器组织。 CALayer位于视图层中。这会导致两个问题:


  1. 您的图层向用户显示您的图层的哪部分?

  2. < <>

    这些问题的答案是:



    现在,对于一个新的应用程序,一个持久模型的最简单的(当然最可扩展的)路径可能是Core Data。


    I have a simple iphone application that paints blocks using a subclass of CALayer and am trying to find the best way to save state or persist the currently created layers.

    I tried using Brad Larson's answer from this previous question on storing custom objects in the NSUserDefaults, which worked for persisting my subclass of CALayer, but not it's basic state like geometry and background color, so they were there but did not render on relaunch.

    I made my declared instance variables conform to the NSCoding protocol but do not know how to make CALayer's properties do the same without re-declaring all of it's properties. Or is this not the correct/best approach altogether?

    Here is the code I'm using to archive the array of layers:

    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:viewController.view.layer.sublayers] forKey:@"savedArray"];
    

    And here is the code I'm using to reload my layers in -viewDidLoad:

        NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    
    NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"savedArray"];
    
    if (dataRepresentingSavedArray != nil) {
    
        [self restoreStateWithData:dataRepresentingSavedArray];
    

    And in -restoreStateWithData:

        NSArray *savedLayers = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    if (savedLayers != nil) {
    
        for(layer in savedLayers) {
    
            [self.view.layer addSublayer:layer];
    
        }
    
        [spaceView.layer layoutSublayers];      
    }
    

    解决方案

    Cocoa (and Cocoa Touch) are mostly based on a model-view-controller organization. CALayers are in the view tier. This leads to two questions:

    1. What part of your model does your layer present to the user?
    2. How do you make that part of the model persist?

    The answers to those questions are your solution.

    Nowadays, for a new app, the simplest (certainly most extensible) path to a persistent model is probably Core Data.

    这篇关于如何使用CALayers保存状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆