我进行了数周的搜索,最后在Github上找到了一个符合我需求的例子。这是一个垃圾拖的例子。我现在根据自己的需要编辑了这个例子。
我现在只有一个问题。在我的Scrollview中,我有5张图片,但它们都是一样的。
我想知道如何将每个缩略图设为不同的图像,因此当我将图像从Scrollview拖动到顶部的Imageview时,它会更改为我从滚动视图拖动的图像。
基本上我想在我的滚动视图中使用不同的图像,就像我一样。
我快到了,我最后一件事就是要完成我的战斗。
一如既往,我们非常感谢任何协助,下面是一些示例图片和我的示例项目的链接。
谢谢。
答案 0 :(得分:3)
这就是我所做的:
一。重写- 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"];
结果:
答案 1 :(得分:0)
注意: 将来,提供您认为与问题特别相关的代码段会很有帮助。发布整个项目和您遇到的问题有点懒惰。你有什么尝试?你在问这里之前尝试过的是什么没有用?
在GalleryButton.m
课程中,initWithFrame:
方法的代码为:
CGRect myImageRect = CGRectMake(0, 0, 64, 64);
images = [[UIImageView alloc] initWithFrame:myImageRect];
[images setImage:[UIImage imageNamed:@"light-cherry.png"]];
[self addSubview:images];
变量名称images
令人困惑,因为它实际上是单个 UIImageView,但这是分配按钮图像的位置。
解决方案是:
initWithFrame:
初始值设定项以接受可传递到UIImage
UIImageView
对象
GalleryButton
的{{1}}或类似内容添加新方法,以便在setImage:(UIImage*)image
对象上设置image
。老实说,如果是我,我会做两件事,除非你不想在创建按钮后修改图像,在这种情况下你应该只实现选项1.