的UIScrollView变焦不工作,自动布局 [英] UIScrollView Zoom Does Not Work With Autolayout

查看:147
本文介绍了的UIScrollView变焦不工作,自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用严格自动布局环境似乎并没有工作,UIScrollView的缩放。

Zooming with UIScrollView using a strictly autolayout environment does not seem to work.

这是特别令人沮丧,因为iOS的6发行说明肯定会导致我相信它应该在的时候写了一个纯自动布局方法在这里<一个href=\"http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html\">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.

我已经看到在互联网上专门针对此问题的唯一问题是,<一个href=\"http://stackoverflow.com/questions/14026807/uiscrollview-zooming-with-auto-layout\">UIScrollView与自动布局缩放,但它不提供问题的code和没有答案。

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.

该用户 http://stackoverflow.com/users/341994/matt 已经给UIScrollView的自动布局许多伟大的回应问题,甚至链接到git的枢纽code,但我一直没能找到任何有回答了这个问题。

This user http://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的发布说明提供的,只是做了最低限度实现缩放样本code。

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版本说明我发现:

Once again, re-reading the iOS SDK 6.0 release notes I found that:

请注意,您可以滚动视图的子视图出现通过创建视图和滚动视图的子树外的视图,如滚动视图的上海华之间的约束浮动(不滚动)比其他滚动内容。

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变焦不工作,自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆