ios - 页面 Controller 中的 ScrollView

标签 ios objective-c uiscrollview uipagecontrol

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

initial

当我放大结果也很好。

zoom in picture

虽然,如果我放大并更改一页并返回, Controller 的行为很糟糕。似乎从上一个 Action 到 scrollView 中还剩下一些东西,我找不到什么。下图是假的。

false image

我想再次获得初始 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/

相关文章:

objective-c - RestKit - 使用空值/空数组映射 JSON 响应

iphone - 从 JSON 中过滤 NSArray?

iphone - UIScrollView - 如何消除滚动之前的延迟?

ios - 在图像的 UIScrollview 上显示 UIButton Done

ios - 在 Swift 中通过不同的类调用委托(delegate)方法

ios - 在 Parse 上存储信息

ios - 使用 SWRevealViewController 时如何登录注销

objective-c - 使用 Swift 的 Epson 打印机找不到 arm64 的符号

ios - 在调整 scrollView 的框架大小时,UIScrollView subview 的大小调整不正确

html - 垂直居中菜单偏移