使用严格的自动布局环境使用 UIScrollView 进行缩放似乎不起作用.
Zooming with UIScrollView using a strictly autolayout environment does not seem to work.
这尤其令人沮丧,因为 iOS 6 发行说明确实让我相信,当我在这里写到纯自动布局方法"时,它应该是 http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html
This is especially frustrating because the iOS 6 release notes certainly lead me to believe it should when the wrote about a "Pure Auto Layout approach" here http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html
我查看了 WWDC 2012 的第 202、228 和 232 场会议的幻灯片,但没有找到答案.
I looked the the WWDC 2012 slides for sessions 202, 228, and 232 and didn't see an answer for this.
我在互联网上看到的唯一一个专门针对此问题的问题是 UIScrollView 自动缩放布局,但没有提供问题代码,也没有答案.
The only question I've seen on the internet specifically for this issue is UIScrollView zooming with Auto Layout, but it doesn't provide code of the problem and there is no answer.
这位用户 https://stackoverflow.com/users/341994/matt 对 UIScrollView 自动布局给出了很多很好的回应问题,甚至链接到 git hub 上的代码,但我在那里找不到任何可以回答这个问题的东西.
This user https://stackoverflow.com/users/341994/matt has given many great responses to UIScrollView autolayout questions and even linked to code on git hub, but I haven't been able to find anything that answers this issue there.
我已尝试将这个问题归结为最低限度以使其清楚.
I have attempted to boil this issue down to the absolute minimum to make it clear.
我创建了一个带有情节提要的新单视图应用程序,并且没有在界面构建器中进行任何更改.
I created a new single view application with a storyboard, and made no changes in the interface builder.
我在项目pic.jpg"中添加了一个大图片文件.
I added a large picture file to the project "pic.jpg".
SVFViewController.h
SVFViewController.h
#import <UIKit/UIKit.h>
@interface SVFViewController : UIViewController <UIScrollViewDelegate>
@property (nonatomic) UIImageView *imageViewPointer;
@end
SVFViewController.m
SVFViewController.m
#import "SVFViewController.h"
@interface SVFViewController ()
@end
@implementation SVFViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *scrollView = [[UIScrollView alloc] init];
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImage:[UIImage imageNamed:@"pic.jpg"]];
[self.view addSubview:scrollView];
[scrollView addSubview:imageView];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
self.imageViewPointer = imageView;
scrollView.maximumZoomScale = 2;
scrollView.minimumZoomScale = .5;
scrollView.delegate = self;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scrollView,imageView);
NSLog(@"Current views dictionary: %@", viewsDictionary);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
}
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageViewPointer;
}
@end
请注意,我特别努力使它与 iOS 6 发行说明中提供的示例代码一样,只是做了最少量的工作来实现缩放.
Notice I made a particular effort to make this as much like the sample code provided in the iOS 6 release notes, just doing the bare minimum to implement zooming.
那么,问题来了?
当您运行此应用程序并在滚动视图中平移时,一切都很好.但是当你缩放时问题很明显,图像来回闪烁,并且每次缩放时图像在滚动视图中的位置都会变得更加错误.
When you run this application and pan around in the scroll view, everything is good. But when you zoom the problem is obvious, the image flickers back and forth, and the placement of the image within the scroll view gets more wrong with every zoom.
看起来 imageView 的内容偏移量正在进行战斗,似乎每次缩放"都会被两个不同的东西设置为不同的值.(imageView 的内容偏移属性的 NSLog 似乎证实了这一点).
It looks like there is battle going on for the content offset of the imageView, it seems it is being set to different values by two different things with every "zoom". (an NSLog of the content offset property of the imageView appears to confirm this).
我在这里做错了什么?有谁知道如何在纯自动布局环境中的 UIScrollView 中实现缩放.那里有这样的例子吗?
What am I doing wrong here? Does anyone know how to property implement zooming within a UIScrollView in an purely autolayout environment. Is there an example of this anywhere out there?
请帮忙.
再次重读iOS SDK 6.0 发行说明 我发现:
请注意,您可以通过在视图和滚动视图的子树之外的视图(例如滚动视图的超级视图)之间创建约束,使滚动视图的子视图看起来浮动(不滚动)在其他滚动内容之上.
Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.
将您的子视图连接到外部视图.换句话说,就是嵌入了滚动视图的视图.
并以以下方式应用约束,我已经成功了:
And applying constraints in following way I've got it work:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView(width)]" options:0 metrics:@{@"width":@(self.imageViewPointer.image.size.width)} views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView(height)]" options:0 metrics:@{@"height":@(self.imageViewPointer.image.size.height)} views:viewsDictionary]];
这篇关于UIScrollView 缩放不适用于自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!