ScrollView在页面控制器中

时间:2013-12-17 12:51:22

标签: ios objective-c uiscrollview uipagecontrol

我尝试制作支持缩放功能的图库。我选择将每个图像放入ViewController的scrollView中,该ViewController用作PageController的页面。这是ViewController的初始图片。

initial

当我放大时,结果也很好。

zoom in picture

虽然,如果我放大并更改一个页面并返回,控制器有一个不好的行为。似乎从前一个动作到scrollView的东西仍然存在,我找不到什么。下面的图片是假的。

false image

我想再次获得初始视图(Pic1)

这些方法处理事件。

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];


self.imageView = [[UIImageView alloc] initWithImage:image];

CGRect lala=(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};

[self.imageView setFrame:lala];


//[self.scrollView setFrame:CGRectMake(0, 65, 320, 520)];

[self.scrollView addSubview:self.imageView];
self.imageView.userInteractionEnabled=YES;

// Tell the scroll view the size of the contents
[self.scrollView setContentSize:  image.size];

// Set up the minimum & maximum zoom scales
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);

self.scrollView.minimumZoomScale = minScale;
self.scrollView.maximumZoomScale = 1.0f;
self.scrollView.zoomScale = minScale;
//[self.scrollView setFrame:self.imageView.frame];
[self.scrollView setFrame:CGRectMake(0, 65, 320, 500)];

[self centerScrollViewContents];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

NSLog(@"Will");

self.imageView = [[UIImageView alloc] initWithImage:image];
//self.scrollView=[[UIScrollView alloc] init];
CGRect lala=(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};

[self.imageView setFrame:lala];

[self.scrollView setFrame:CGRectMake(0, 65, 320, 500)];
//[self.scrollView setFrame:self.imageView.frame];

//[self.scrollView addSubview:self.imageView];
self.imageView.userInteractionEnabled=YES;

// Tell the scroll view the size of the contents
[self.scrollView setContentSize:  image.size];

}

- (void)centerScrollViewContents {
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imageView.frame;

if (contentsFrame.size.width < boundsSize.width) {
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
    contentsFrame.origin.x = 0.0f;
}

if (contentsFrame.size.height < boundsSize.height) {
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
    contentsFrame.origin.y = 0.0f;
}

self.imageView.frame = contentsFrame;}

1 个答案:

答案 0 :(得分:0)

viewWillAppear:中,您正在创建图像视图的新实例,并在viewDidAppear:中将其添加到滚动视图中。当您返回此视图控制器时,将再次调用这些方法。因此,再次添加新图像。 实际上,您正在使用这两种方法初始化图像视图。

希望这有帮助。