在Superview中存储UIImageViews及其位置 [英] Storing UIImageViews and their locations in the superview

查看:84
本文介绍了在Superview中存储UIImageViews及其位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How are UIImageViews stored/serialized so that they can be loaded upon app relaunch?

此外,我想将它们的当前位置与每个图像一起存储在超视图中。如何检测到这种情况?

Also I would like to store their current position in the superview along with each image. How is this detected?

谢谢

推荐答案

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

I did it by creating a separate class (I call it an image view manager) to hold the image view properties such as position, image name, etc. and then subclassed UIImageView to have a reference to a instance of the image view manager and update its properties when something in the image view changes, such as location. Then you just have to serialize the image managers and when you restart recreate the image views from the information in each manager (though I used core data since this was part of a larger core data application).

经理类:

#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:

----界面-----

---- Interface -----

#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

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

--- Implementation ---------

#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];
}

这篇关于在Superview中存储UIImageViews及其位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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