我想在同一个视图上有两个可缩放的UIImageViews,但我无法让它工作!

时间:2011-03-07 03:25:24

标签: iphone uiscrollview uiimageview nested zoom

好的,我一直在研究这个功能已经有一段时间了,这让我疯了。我在网上看了一遍,似乎找不到任何答案。我希望有一个包含两个UIScrollviews的视图,每个视图本身都包含一个UIImageView。所以我可以对我的视图中的图像进行单独滚动/缩放。

看起来很简单,但我无法让它发挥作用。我在IB中设置了ScrollView / ImageView对并将它们连接好了。现在我的viewDidLoad代码如下所示:

-(void) viewDidLoad
{
// set the image to be displayed, pic your own image here

imageView = [[MyImage alloc] initWithImage: [UIImage imageNamed: @"newBackground.png"]];

// yes we want to allow user interaction

[imageView setUserInteractionEnabled:YES];

// set the instance of our myScrollView to use the main screen

scrollView = [[MyScroll alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

// turn on scrolling

[scrollView setScrollEnabled: YES];

// set the content size to the size of the image

[scrollView setContentSize: imageView.image.size];

// add the instance of our myImageView class to the content view

[scrollView addSubview: imageView];

// flush the item

[imageView release];

// set max zoom to what suits you

[scrollView setMaximumZoomScale:1.0f];

// set min zoom to what suits you

[scrollView setMinimumZoomScale:0.25f];

// set the delegate

[scrollView setDelegate: self];

// scroll a portion of image into view (my image is very big) :)

//[scrollView scrollRectToVisible:CGRectMake(100, 100, 320, 440) animated:NO];

// yes to autoresize

scrollView.autoresizesSubviews = YES;

// set the mask

scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

// set the view

//self.view =scrollView;

[scrollView setFrame:CGRectMake(0, 0, 320, 240)];

self.view =scrollView;

imageView1 = [[MyImage alloc] initWithImage: [UIImage imageNamed: @"newBackground.png"]];

// yes we want to allow user interaction

[imageView1 setUserInteractionEnabled:YES];

// set the instance of our myScrollView to use the main screen

scrollView1 = [[MyScroll alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

// turn on scrolling

[scrollView1 setScrollEnabled: YES];

// set the content size to the size of the image

[scrollView1 setContentSize: imageView1.image.size];

// add the instance of our myImageView class to the content view

[scrollView1 addSubview: imageView1];

// flush the item

[imageView1 release];

// set max zoom to what suits you

[scrollView1 setMaximumZoomScale:1.0f];

// set min zoom to what suits you

[scrollView1 setMinimumZoomScale:0.25f];

// set the delegate

[scrollView1 setDelegate: self];

// scroll a portion of image into view (my image is very big) :)

//[scrollView scrollRectToVisible:CGRectMake(100, 100, 320, 440) animated:NO];

// yes to autoresize

scrollView1.autoresizesSubviews = YES;

// set the mask

scrollView1.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

// set the view

//self.view =scrollView;

[scrollView1 setFrame:CGRectMake(0, 300, 320, 220)];

//I do this only because I don't know what else to do. This effectively adds the scrollview to the scrollview...
self.view =scrollView1;

第一个scrollView加载,但第二个嵌套在那个!我试过[self.view addSubView:scrollView1];,但这只会让应用程序崩溃。我正尽力去挖掘并尽可能地找出scrollViews,但我只是在这里拥有。

请帮忙!

由于

**为了进一步说明,我想要的是在图像的上半部分上有一个scrollView,它可以使用图像内部的图像进行平移和缩放,然后另一个与可以平移和放大的图像分开的scrollView屏幕的下半部分,独立于第一个。**

1 个答案:

答案 0 :(得分:1)

您需要将两个滚动视图添加为ViewController视图(self.view)的子视图:

[self.view addSubview:scrollView];
[self.view addSubview:scrollView1];

然后你需要手动定位这些视图,使它们的位置与你想要的一样(上面的代码会将两个滚动视图放在一起,有一些任意的默认大小,可能是也可能不是你想要的。)