在使用纯自动布局的环境下,使用UIScrollView进行缩放似乎不起作用。
这尤其令人沮丧,因为iOS 6版本说明肯定让我相信它应该在这里的“纯自动布局方法”中起作用:http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html
我查看了WWDC 2012年的演示文稿,包括202、228和232会话,但没有找到答案。
互联网上唯一与此问题相关的问题是UIScrollView zooming with Auto Layout,但它没有提供问题的代码,也没有答案。
这位用户https://stackoverflow.com/users/341994/matt对UIScrollView自动布局问题给出了很多很好的回答,甚至链接到了git hub上的代码,但我无法找到任何解决此问题的内容。
我已经试图将这个问题简化到绝对最小,以使它清晰明了。
我创建了一个带有storyboard的新单视图应用程序,在接口生成器中没有进行任何更改。
我向项目添加了一个大的图片文件“pic.jpg”。
SVFViewController.h
#import <UIKit/UIKit.h>
@interface SVFViewController : UIViewController <UIScrollViewDelegate>
@property (nonatomic) UIImageView *imageViewPointer;
@end
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发布说明提供的示例代码尽可能相似,只是做了最低限度的实现缩放。
那么问题呢?
当您运行此应用程序并在滚动视图中移动时,一切都很好。但当您进行缩放时,问题就显而易见了,图像会来回闪烁,并且图像在滚动视图中的位置每次缩放时都会变得更加错误。
看起来imageView的内容偏移量正在发生冲突,似乎两个不同的事物每次“缩放”时都将其设置为不同的值。(imageView内容偏移属性的NSLog似乎证实了这一点)。
我在这里做错了什么?有人知道如何在纯自动布局环境中正确实现UIScrollView的缩放吗?是否有任何相关示例可供使用?
请帮忙解决。