滚动视图中的全屏图像

时间:2013-07-23 10:17:10

标签: ios uiscrollview uiimageview

我有一个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);
}

1 个答案:

答案 0 :(得分:1)

那么你不应该添加UIImageViews。

而是添加自定义类型的UIButton并将图像添加到按钮并向UIButtons添加IBAction,这会在您的图像中添加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];
}