0

UIScrollView在那个 ScrollView 中有一个并且有图像,它们是通过编程方式添加的。当我单击图像时,我希望该图像全屏显示。

我应该在我的代码中添加什么?
图片数量取决于[图片数量];

self.scrollView.contentSize = CGSizeMake(320, 134);
self.scrollView.delegate = self;
int X=0;

for (int i = 0; i < [images count]; i++)
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 140, 136)] ;
    imageView.backgroundColor = [UIColor redColor];
    [imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]]];
    [imageView setImage: [images objectAtIndex:i]];
    [self.scrollView addSubview: imageView];
    X = X + imageView.frame.size.height+5;

    if(X > 320)
        self.scrollView.contentSize = CGSizeMake(X, 134);
}
4

1 回答 1

1

你不应该添加 UIImageViews 。

而是添加UIButton自定义类型并将图像添加到按钮并添加IBAction到 UIButtons,这会为您的图像添加 FullScreenView。

在您for()的每个按钮中添加一个带有 i 的标签,并在您的 IBAction 函数中获取标签[sender tag]并获取您的图像[images objectAtIndex:[sender tag]]

例子:

-(void)test{

for (int i = 0; i < [images count]; i++)
{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(X, 0, 140, 136);
    [button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
    button.tag = i;
    [button addTarget:self action:@selector(showFullscreen:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:button];
    X = X + button.frame.size.height+5;

    if(X > 320)
        self.scrollView.contentSize = CGSizeMake(X, 134);
}

}

-(IBAction)showFullscreen:(id)sender{

UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)] ;
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [images objectAtIndex:[sender tag]]];
[self.view addSubview: imageView];
}
于 2013-07-23T10:29:53.033 回答