Ios5 iOS 5屏幕上对象的位置

Ios5 iOS 5屏幕上对象的位置,ios5,uiimageview,nsuserdefaults,Ios5,Uiimageview,Nsuserdefaults,我正在创建一个测试应用程序,它将添加多个带有图像的UIImageView。用户将能够移动和旋转这些图像。我已经准备好UIgestureRecognitors并开始工作,但我还需要跟踪用户在屏幕上留下图像的位置。这样,如果他们关闭应用程序并返回,他们放置图像的位置将被记住 我知道我应该为此使用NSUserDefaults,但我的问题是如何跟踪屏幕上可能存在的大量UIImageView位置。我假设我需要以某种方式获得它们的x/y坐标,并使用默认值存储它们 有人对如何做到这一点有建议吗 -BrianU

我正在创建一个测试应用程序,它将添加多个带有图像的UIImageView。用户将能够移动和旋转这些图像。我已经准备好UIgestureRecognitors并开始工作,但我还需要跟踪用户在屏幕上留下图像的位置。这样,如果他们关闭应用程序并返回,他们放置图像的位置将被记住

我知道我应该为此使用NSUserDefaults,但我的问题是如何跟踪屏幕上可能存在的大量UIImageView位置。我假设我需要以某种方式获得它们的x/y坐标,并使用默认值存储它们

有人对如何做到这一点有建议吗


-Brian

UIView有一个属性子视图。我建议在数组中循环。以下是一个例子:

NSMutableDictionary *coordinates = [[NSMutableDictionary alloc] init];
for (id subview in view.subviews) {
    if ([subview isKindOfClass:[UIImageView class]]) {
        [coordinates setObject:[NSValue valueWithPoint:subview.frame.origin] forKey:imageViewIdentifier];
    }
}
//store coordinates in NSUserDefaults
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:coordinates forKey:@"ImageViewCoordinates"];
[userDefaults synchronize];
不必将整个图像视图作为键存储在坐标字典中,您可以为其使用一些标识符来节省内存。该对象是NSValue,因此要从中获取x/y值,可以使用[[value pointValue]x]或[[value pointValue]y]

下面是一个将数据读回并将视图恢复正常的示例

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *coordinates = [userDefaults dictionaryForKey:@"ImageViewCoordinates"];
//Key can be any type you want
for (NSString *key in coordinates.allKeys) {
    UIImageView *imageView;
    //Set UIImageView properties based on the identifier
    imageView.frame.origin = [coordinates objectForKey:key];
    [self.view addSubview:imageView];
    //Add gesture recognizers, and whatever else you want
}

UIView具有属性子视图。我建议在数组中循环。以下是一个例子:

NSMutableDictionary *coordinates = [[NSMutableDictionary alloc] init];
for (id subview in view.subviews) {
    if ([subview isKindOfClass:[UIImageView class]]) {
        [coordinates setObject:[NSValue valueWithPoint:subview.frame.origin] forKey:imageViewIdentifier];
    }
}
//store coordinates in NSUserDefaults
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:coordinates forKey:@"ImageViewCoordinates"];
[userDefaults synchronize];
不必将整个图像视图作为键存储在坐标字典中,您可以为其使用一些标识符来节省内存。该对象是NSValue,因此要从中获取x/y值,可以使用[[value pointValue]x]或[[value pointValue]y]

下面是一个将数据读回并将视图恢复正常的示例

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *coordinates = [userDefaults dictionaryForKey:@"ImageViewCoordinates"];
//Key can be any type you want
for (NSString *key in coordinates.allKeys) {
    UIImageView *imageView;
    //Set UIImageView properties based on the identifier
    imageView.frame.origin = [coordinates objectForKey:key];
    [self.view addSubview:imageView];
    //Add gesture recognizers, and whatever else you want
}