以编程方式向UIScrollview添加UIButtons和UIImages

3
我已经修改了苹果公司的Scrolling示例,以获取从CoreData对象引用的文件创建的一系列图像。我可以很好地添加这些图片,但我还想在每个图像上以编程方式添加一个UIButton。图片的数量取决于CoreData对象的数量,因此我不能使用IB。
有人能给我一些帮助,告诉我如何最好地实现这个功能吗?
以下是从CoreData存储库中获取图像的代码:
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
    Sentence *testSentence = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:i];

    NSArray *paths       = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *imageName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[testSentence image]];
    NSLog(@"imageName: %@", imageName);

    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageName];
    NSLog(@"image: %@", image);

    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    NSLog(@"imageView: %@", imageView);

    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    [imageView setBackgroundColor:[UIColor blackColor]];

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    CGRect rect = imageView.frame;
    rect.size.height = kScrollObjHeight;
    rect.size.width = kScrollObjWidth;
    imageView.frame = rect;
    imageView.tag = i;  // tag our images for later use when we place them in serial fashion
    [scrollView1 addSubview:imageView];

    [image release];
    [imageView release];
}

[self layoutScrollImages];  // now place the photos in serial layout within the scrollview

以下是在滚动视图中布置图像的代码:
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];

// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
    if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
    {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        view.frame = frame;

        curXLoc += (kScrollObjWidth);
    }
}

// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
2个回答

1

我曾经遇到过同样的问题,我正在创建一个图像库,但是我添加到每个缩略图上的按钮无法响应 TouchUpInside 事件。

通过创建自定义 UIView 子类来容纳每个单独的图像,并在该子类中实现 touchesBegan:withEvent: 和 touchesEnded:withEvent:,我成功解决了这个问题。

请参阅 UIScrollView 编程指南 获取示例代码。

如果您不允许在 UIScrollView 上进行缩放,则可以简化代码,假设每个触摸结束都是单个触摸事件。


1

不确定我是否理解了您的问题。您不能只是这样做吗?

...
 imageView.frame = rect;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = imageView.frame;
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:aButton];

谢谢你的回答。是的,那个方法可以用 - 但是我无法点击按钮(它坚定地忽略我的点击/轻敲 - 是的,我已经更改了操作),而且我想要一个大小不同于imageView.frame的按钮。有什么想法吗? - glenstorey
你可能是指 aButton.frame = imageView.bounds,虽然在这种情况下我认为这并不重要。你可能还需要设置 imageView.userInteractionEnabled=YES(默认情况下 UIImageView 的值为 NO,如果我没记错的话)。 - tc.
已经解决了按钮/图像框大小的问题,但是我无法使其点击。我尝试过: [aButton setUserInteractionEnabled:YES]; [aButton setEnabled:YES]; [aButton setAlpha:1]; 但似乎没有任何进展。有没有一种调试你正在点击的方法?有什么策略可以让事情正常工作吗? - glenstorey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接