在 superview 中存储 UIImageViews 及其位置
Posted
技术标签:
【中文标题】在 superview 中存储 UIImageViews 及其位置【英文标题】:Storing UIImageViews and their locations in the superview 【发布时间】:2011-03-24 00:05:15 【问题描述】:如何存储/序列化 UIImageView 以便在应用重新启动时加载它们?
我还想将它们的当前位置与每张图像一起存储在超级视图中。这是如何检测到的?
谢谢
【问题讨论】:
【参考方案1】:我通过创建一个单独的类(我称之为图像视图管理器)来保存图像视图属性,例如位置、图像名称等,然后将 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];
【讨论】:
【参考方案2】:这可能比我原来的答案更好地回答你的问题:iPhone - save UIImageView state
【讨论】:
以上是关于在 superview 中存储 UIImageViews 及其位置的主要内容,如果未能解决你的问题,请参考以下文章
在 LongPressEnded 之后迅速从 SuperViews 中删除视图