我尝试制作一个支持缩放功能的图片库。我选择将每个图像放入 ViewController 的 scrollView 中,用作 PageController 的页面。这是 ViewController 的初始图片。
当我放大结果也很好。
虽然,如果我放大并更改一页并返回, Controller 的行为很糟糕。似乎从上一个 Action 到 scrollView 中还剩下一些东西,我找不到什么。下图是假的。
我想再次获得初始 View (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;}
最佳答案
在 viewWillAppear:
您正在创建 ImageView 的新实例,并且在 viewDidAppear:
中您正在将其添加到 ScrollView 中。当你回到这个 View Controller 时,这些方法会再次被调用。因此,新图像被再次添加。
实际上,您正在两种方法中初始化 ImageView 。
希望这可以帮助。
关于ios - 页面 Controller 中的 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20634664/