これが答えです。UIViewに複数のUIImageViewを追加する方法。画像のリストから。
以下のコードは現在iOS6で動作しています。
CreateViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "Item.h"
#import "PickTableViewController.h"
#import "CreateViewController.h"
@interface CreateViewController : UIViewController {
CGPoint startLocation;
}
@property(nonatomic, strong)UIView *createView;
@property(nonatomic, strong)UIImageView *bgImgView;
@property(nonatomic, strong) Item *currentItem;
@property(nonatomic, strong)UIImage *myPick;
@property(nonatomic, strong)UIImage *myImage;
@property(nonatomic, strong)NSMutableArray *imageViews;
@property(nonatomic, strong)NSMutableArray *imageViewItems;
CreateViewController.m
// create a view on top of base view controller.
CGRect myFrame = CGRectMake(0,-20,320,480);
self.createView = [[UIView alloc] initWithFrame:myFrame];
[self.view addSubview:self.createView];
// This is an pic chosen by user from camera or library or default image.
self.bgImgView = [[UIImageView alloc] initWithImage:image];
[self.createView addSubview:self.bgImgView];
// currentItem is set in TableViewController and passed via prepareforsegue.
NSString *cat = currentItem.category;
NSString *file = currentItem.filename;
// add images to "imageViewItems" array.
imageViewItems = [[NSMutableArray alloc] init];
Item *img = [[Item alloc] init];
[img setCategory:cat];
[img setFilename:file];
[imageViewItems addObject:img];
img = [[Item alloc] init];
[img setFilename:@"catone-item-0.png"];
[imageViewItems addObject:img];
img = [[Item alloc] init];
[img setCategory:@"catone"];
[img setFilename:@"catone-item-1.png"];
[imageViewItems addObject:img];
// create array of uiimageviews from array of images.
CGRect frame = self.view.bounds;
CGRect _myFrame = frame;
for (int i = 0; i < [imageViewItems count]; i++) {
// get array item.
Item *arrayItem = [imageViewItems objectAtIndex:i];
// get filename from array item.
NSString *curpick = arrayItem.filename;
// create UIImage from array item filename.
image = [UIImage imageNamed:curpick];
// create UIImageView with UIImage
ItemUIImageView *imageView = [[ItemUIImageView alloc] initWithImage:image];
imageView.frame = _myFrame;
imageView.userInteractionEnabled = YES;
// add subview to createView.
[self.createView addSubview:imageView];
// add UIImageView to array.
[self.imageViews addObject:imageView];
}
これは、ドラッグ可能な画像用にCreateViewController.hおよび.mで参照されるサブクラスUIImageViewファイルです。
ItemUIImageView.h
#import <UIKit/UIKit.h>
@interface ItemUIImageView : UIImageView {
CGPoint startLocation;
}
@end
ItemUIImageView.m
#import "ItemUIImageView.h"
@implementation ItemUIImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self];
CGFloat dx = pt.x - startLocation.x;
CGFloat dy = pt.y - startLocation.y;
CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy);
self.center = newCenter;
}
@end
このファイルは、画像オブジェクト情報を保存するためのカスタムクラスです。
Item.h
#import <Foundation/Foundation.h>
@interface Item : NSObject
@property (nonatomic, strong) NSString *filename;
@property (nonatomic, strong) NSString *category;
@end
Item.m
#import "Item.h"
@implementation Item
@synthesize filename, category;
@end
ロブが助けた後、私が自分の質問に答えるかどうかはわかりません。スタックで完全な例を見つけられなかったので、ここに私のものがあります。