我正在用双击放大UIImageView在UIScrollView中。在Interface中设置它时工作正常。我在这里看到的神奇之处是,当我放大/缩小时,contentSize of scrollView会自动增加/减少。我检查contentSize在viewDidLoad,它不是零。
通过从IB中移除scrollView和imageView,我也尝试了同样的方法,并以编程方式创建它们。在添加imageView和tapGestureRecognizer之后,我无法放大这里。当我检查原因时,contentSize在任何地方都保持为零。
当我将它们添加到IB/xib中时,我对contentSize不做任何操作。我不会在任何地方设置它。我发现它很好,contentSize会在我需要的地方自动调整。但是,当我以编程方式创建scrollView时,它并不是在复制。
我怎么才能让它起作用?我欢迎你的建议。
这是我的代码供参考。
在IB中创建的ScrollView
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTap];
imageScrollView.minimumZoomScale=1.01;
imageScrollView.maximumZoomScale=5;
imageScrollView.zoomScale=1.01;
NSLog(@"height = %f width = %f", imageScrollView.contentSize.height, imageScrollView.contentSize.width);
}NSLog说
height = 449.479767 width = 320.000000以编程方式创建的ScrollView
- (void)viewDidLoad {
[super viewDidLoad];
imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTap];
imageScrollView.minimumZoomScale=1.01;
imageScrollView.maximumZoomScale=5;
imageScrollView.zoomScale=1.01;
[self.view addSubview:imageScrollView];
[self.imageScrollView addSubview:imageView];
NSLog(@"height = %f width = %f", imageScrollView.contentSize.height, imageScrollView.contentSize.width);
}NSLog说
height = 0.000000 width = 0.000000发布于 2013-07-25 07:51:17
这些性质
self.minimumZoomScale=1.01;
self.maximumZoomScale=5;
self.zoomScale=1.01;应该是scrollView的属性:
imageScrollView.minimumZoomScale=1.01;
imageScrollView.maximumZoomScale=5;
imageScrollView.zoomScale=1.01;此外,还需要设置contentSize属性(在使用IB时不需要设置它,因为它是从xib文件加载的):
imageScrollView.contentSize = CGSizeMake(width,height);使用适当的值。
此外,还需要设置委托:
imageScrollView.delegate = self;并且至少有一个委托方法指示要缩放哪个子视图:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return scrollView.subviews[0];
}(实际上,最好在子视图上设置标记,或者将其分配给属性以获得对其的引用,因为scrollViews也有内置子视图)
发布于 2013-07-25 08:07:17
您的xib中启用了自动布局吗?如果是这样,IB将在图像视图和滚动视图之前设置约束,并且在运行时自动布局使用约束来设置滚动视图的内容大小。在代码中创建视图时,没有添加足够的约束,也没有直接设置内容大小,因此内容大小保持为零。
有关更多信息,请阅读科技笔记TN2154。
发布于 2013-07-26 07:33:26
https://stackoverflow.com/questions/17851568
复制相似问题