这是答案;如何向一个 UIView 添加多个 UIImageView;从图像列表中。
以下代码目前在 iOS6 中运行:
创建视图控制器.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;
创建视图控制器.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];
}
这是子类 UIImageView 文件,在 CreateViewController.h 和 .m 中引用,用于可拖动图像。
项目UIImageView.h
#import <UIKit/UIKit.h>
@interface ItemUIImageView : UIImageView {
CGPoint startLocation;
}
@end
项目UIImageView.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
该文件是用于存储图像对象信息的自定义类。
项目.h
#import <Foundation/Foundation.h>
@interface Item : NSObject
@property (nonatomic, strong) NSString *filename;
@property (nonatomic, strong) NSString *category;
@end
项目.m
#import "Item.h"
@implementation Item
@synthesize filename, category;
@end
我不确定在 Rob 帮助之后我是否要回答我自己的问题。我没有在堆栈中找到完整的示例,所以这是我的。