2

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

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

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

4

2 回答 2

2

您应该使用 aUIScrollView并将其添加UIImageView为 contentView 的子视图。然后将scrollview的contentsize设置为imageviews的大小。

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

目前没有要测试的 Mac,但这应该可以。

于 2012-09-15T10:25:04.200 回答
0

我基本上同意西加写的。您的 imageView 必须是 scrollView 的子视图。然后,别忘了设置scrollView的contentSize & minimum&maximumZoomScale & 和imageView的frame:

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{}

因此,它会在每次视图出现以及设备旋转时执行。

于 2012-09-15T15:20:04.880 回答