使用ImageView iOS滚动图像

时间:2012-09-15 10:19:22

标签: ios imageview

我正在尝试为餐馆制作应用程序并将食物菜单显示为可滚动图像。图像从配置文件中提取。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // back button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"btn-back.png"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0.0f, 0.0f, 46.0f, 28.0f);
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;
    [customBarItem release];


    [self setTitle:[menu objectForKey:@"title"]];
    [scrollView2 setScrollEnabled:YES];

    NSString *imageName = [menu objectForKey:@"imageName"];
    if (![imageName isEqualToString:@""]) {
        UIImage *image = [UIImage imageNamed:imageName];

        [imageView setImage:image];
        [scrollView2 addSubview:imageView];
        [imageView release];
    }
    [textView setText:[menu objectForKey:@"description"]];
}

截至目前,图像不会滚动,但会显示为静态图像。任何人都可以帮我修改这段代码,以便用户可以垂直滚动图像。 非常感谢。

2 个答案:

答案 0 :(得分:2)

您应该使用UIScrollView并添加UIImageView作为contentView的子视图。 然后将scrollview的内容大小设置为imageviews大小。

[imageView setImage:image];
[scrollView2.contentView addSubview:imageView];
[scrollView2  setContentSize:image.frame.size];
[imageView release];

目前没有Mac可供测试,但这应该可行。

答案 1 :(得分:0)

我主要同意Seega所写的内容。您的imageView必须是scrollView的子视图。然后,不要忘记设置scrollView的contentSize&最小和最大缩放尺度&和imageView的框架:

self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);

self.scrollView.minimumZoomScale = // some number between 0 and 1, default is 1, so no zoom
self.scrollView.maximumZoomScale = // some number larger than 1, default is 1, so no zoom
self.scrollView.zoomScale = 1 // starting zoom scale

放置此代码的好地方是

-(void)viewWillLayoutSubviews{}

因此,每当视图出现时它都会被执行。也可以在设备旋转时使用。