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
}