我进行了数周的搜索,终于在 Github 上找到了一个适合我需要的示例。这是一个垃圾拖动示例。我现在已经根据我的需要编辑了这个例子。
我现在只有一个问题。在我的 Scrollview 中,我有 5 张图片,但它们都是一样的。
我想知道如何使每个缩略图成为不同的图像,因此当我将图像从 Scrollview 拖动到顶部的 Imageview 时,它会更改为我从 ScrollView 中拖动的图像。
基本上我希望我的 ScrollView 中的不同图像与我所拥有的不同。
我快到了,我被困在完成战斗所需的最后一件事上。
一如既往的任何帮助将不胜感激,下面是一些示例图像和指向我的示例项目的链接。
谢谢你。
MySampleProject
最佳答案
这就是我所做的:
一。重写- 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"];
结果:
关于iphone - 将图像数组添加到 ScrollView Objective-C ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13334789/