在Superview中存储UIImageViews及其位置

时间:2011-03-24 00:05:16

标签: iphone

UIImageViews如何存储/序列化,以便在重新启动应用程序时加载它们?

另外,我想将它们的当前位置与每个图像一起存储在超视图中。怎么检测到这个?

由于

2 个答案:

答案 0 :(得分:2)

我是通过创建一个单独的类(我称之为图像视图管理器)来保存图像视图属性(如位置,图像名称等),然后将UIImageView子类化为对图像视图实例的引用管理器并在图像视图中的某些内容发生更改(例如位置)时更新其属性。然后你只需要序列化图像管理器,并在重新启动时从每个管理器中的信息重新创建图像视图(虽然我使用了核心数据,因为这是更大的核心数据应用程序的一部分)。

经理班级:

#import <CoreData/CoreData.h>


@interface ImageViewManager :  NSManagedObject  
{
}

@property (nonatomic, retain) NSNumber * imageLastY;
@property (nonatomic, retain) NSNumber * imageZorder;
@property (nonatomic, retain) NSNumber * imageLastX;
@property (nonatomic, retain) NSString * imageName;

@end

<强> UIImageViewClass:

----界面-----

#import <UIKit/UIKit.h>
#import "ImageViewManager.h"

@protocol MyImageViewDelegate
-(void)didUpdateImageView:(UIImageView *)imageView;
@end

@interface MyImageView : UIImageView {

    ImageViewManager *imageViewManager;
    id <MyImageViewDelegate> delegate;

}   
@property (nonatomic, retain) ImageViewManager *imageViewManager;
@property (nonatomic, assign) id <MyImageViewDelegate> delegate;
-(id)initWithImage:(UIImage *)image andImageViewManager:(ImageViewManager *)ivm;
@end

---实施---------

#import "MyImageView.h"


@implementation MyImageView
@synthesize imageViewManager;
@synthesize delegate;

- (id)initWithImage:(UIImage *)image andImageViewManager:(ImageViewManager *)ivm {

    if (self = [super initWithImage:image]) {
        self.userInteractionEnabled = YES;
        self.imageViewManager = ivm;
    }

    return self;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    [self.superview bringSubviewToFront:self];
    CGPoint prevlocation = [touch previousLocationInView:self.superview];
    CGPoint location = [touch locationInView:self.superview];

    location.x = (self.center.x - (prevlocation.x - location.x));
    location.y = (self.center.y - (prevlocation.y - location.y));
    self.center = location;
    self.imageViewManager.imageLastX = [NSNumber numberWithDouble:self.center.x];
    self.imageViewManager.imageLastY = [NSNumber numberWithDouble:self.center.y];

    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.delegate didUpdateImageView:self];
}   

- (void)dealloc {
    [imageViewManager release];
    [super dealloc];
}

答案 1 :(得分:0)

这可能比我原来的答案更好地回答您的问题:iPhone - save UIImageView state