【发布时间】:2013-01-04 04:21:04
【问题描述】:
使用严格的自动布局环境使用 UIScrollView 进行缩放似乎不起作用。
这尤其令人沮丧,因为 iOS 6 发行说明确实让我相信,当我在 http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html987654321@ 上写到“纯自动布局方法”时,我应该相信这一点。
我查看了 WWDC 2012 的第 202、228 和 232 场会议的幻灯片,但没有找到答案。
我在互联网上看到的唯一一个专门针对这个问题的问题是UIScrollView zooming with Auto Layout,但它没有提供问题的代码,也没有答案。
这位用户https://stackoverflow.com/users/341994/matt 对 UIScrollView 自动布局问题给出了很多很好的回应,甚至链接到 git hub 上的代码,但我在那里找不到任何可以回答这个问题的东西。
我已尝试将这个问题归结为最低限度以使其清楚。
我创建了一个带有情节提要的新单视图应用程序,并且没有在界面构建器中进行任何更改。
我在项目“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 中实现缩放。那里有这样的例子吗?
请帮忙。
【问题讨论】:
-
我遇到了这个 (developer.apple.com/library/ios/#samplecode/PhotoScroller/…),这比回答我的问题更让我担心。是的,它使用自动布局实现 UIScrollView 缩放,但它是通过以一种我无法理解的复杂方式子类化 UIScrollView 来实现的。必须有一种更简单的方法来做到这一点......
-
我遇到了同样的问题。在 UIScrollView 中嵌入 UIImageView 并不像现在使用自动布局那样简单。
标签: ios uiscrollview zooming autolayout