【问题标题】:Add Array of Images to a UIScrollView ObjectiveC将图像数组添加到 UIScrollView 目标 C
【发布时间】:2012-11-11 19:50:56
【问题描述】:

我进行了数周的搜索,终于在 Github 上找到了一个适合我需要的示例。这是一个垃圾拖动示例。我现在已经根据需要编辑了示例。

我现在只有一个问题。在我的 Scrollview 中,我有 5 张图片,但它们都是一样的。

我想知道如何使每个缩略图成为不同的图像,因此当我将图像从 Scrollview 拖到顶部的 Imageview 时,它会更改为我从滚动视图中拖出的图像。

基本上,我希望滚动视图中的不同图像与我拥有的不同。

我快到了,但我被困在完成战斗所需的最后一件事上。

如往常一样,我们将不胜感激,以下是一些示例图片和我的示例项目的链接。

谢谢。

MySampleProject

【问题讨论】:

    标签: iphone objective-c ios xcode drag-and-drop


    【解决方案1】:

    这就是我所做的:

    一个。像这样重写GalleryButton类的- initWithFrame:方法:

    - (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;
    }
    

    然后,像这样重写GalleryScrollView类的- addAttachment:方法:

    - (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"];
    

    结果:

    【讨论】:

    • 为什么我会在这一行得到一个错误:GalleryButton *btnAttachment = [[GalleryButton alloc] init:CGRectMake(startX, startY, width, height) imageName:imgName];在我的 GalleryScrollView.m "实例方法 -init:imageNamed not found?
    • 如何在我的头文件中声明这个?
    • 它现在可以工作,但我在 HegakaDragAndDropRecycleBinViewController viewdidload add attachement withImageNamed not found 中收到警告。与我的图像 [self.gallery addAttachment:item withImageNamed:@"recyclebin"];
    • (也要更改方法声明...请按常识!)
    • 这也有效:) AttachmentItem *item = [[AttachmentItem alloc] initWithData:1 data:nil]; [self.gallery addAttachment:item]; [项目发布]; AttachmentItem *item2 = [[AttachmentItem alloc] initWithData:2 data:nil]; [self.gallery addAttachment:item2]; [项目2发布];
    【解决方案2】:

    注意: 将来,提供您认为与问题特别相关的代码的 sn-ps 对您会有所帮助。发布整个项目和您遇到的问题有点懒惰。你试过什么?你在这里问之前尝试过什么,什么没用?

    在您的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,但这是分配按钮图像的位置。

    解决方案是:

    1. 更改initWithFrame: 初始化程序,使其也接受可以传递给UIImageViewUIImage 对象
    2. 向您的GalleryButton 添加一个名为setImage:(UIImage*)image 或类似方法的新方法,用于在您的UIImageView 对象上设置image

    老实说,如果是我,我会两者都做,除非您不希望在创建按钮后修改该图像,在这种情况下您应该只实施选项 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 2012-01-20
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      相关资源
      最近更新 更多