这就是我所做的:
一。像这样重写类的- initWithFrame:
方法GalleryButton
:
- (id)initWithFrame:(CGRect)frame imageName:(NSString *)imgName
{
self = [super initWithFrame:frame];
if (self) {
isInScrollview = YES;
CGRect myImageRect = CGRectMake(0, 0, 64, 64);
images = [[UIImageView alloc] initWithFrame:myImageRect];
// here's the change:
// instead of a constant name, use the `imgName` parameter
[images setImage:[UIImage imageNamed:imgName]];
[self addSubview:images];
self.backgroundColor = [UIColor blackColor];
self.layer.borderWidth = 2;
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 5;
}
return self;
}
然后,像这样重写类的- addAttachment:
方法GalleryScrollView
:
- (void) addAttachment:(AttachmentItem *)attachment withImageNamed:(NSString *)imgName
{
// everything stays the same (!), except this line:
GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height)];
// is to be extended to:
GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height) imageName:imgName];
...
}
然后,在 中- [HegakaDragAndDropRecycleBinViewController viewDidLoad]
,指定要使用的图像的文件名:
[self.gallery addAttachment:item withImageNamed:@"recyclebin"];
[self.gallery addAttachment:item withImageNamed:@"canadian-maple"];
[self.gallery addAttachment:item withImageNamed:@"light-cherry"];
[self.gallery addAttachment:item withImageNamed:@"mozambique-wenge"];
[self.gallery addAttachment:item withImageNamed:@"canadian-maple"];
结果: