IOS ScrollView问题

时间:2013-05-14 14:40:08

标签: ios iphone uiscrollview matrix-transform

嘿,我正在关注这个tutorial,目前正处于第一步,但我遇到了一些奇怪的行为。本教程是为图像创建滚动视图,并以显示如何使用页面控制器创建分页水平ScrollView来结束。我跟着第一节,事情似乎工作得很好。但是,当我运行代码时,它没有正常运行,经过大约一个小时的调试后我下载了最终项目,在我的手机上运行它以检查它是否正常工作(它是)然后将代码复制到我所需的文件中项目。我的项目仍然无法运行。所以我认为它必须是我在项目设置中配置故事板的方式。然后,我打开了工作示例,清除了他们的故事板并创建了视图,并以我在项目中完成的相同方式和顺序完成了自我。巴姆!它仍然有效,当我删除故事板中的所有视图并以完全相同的方式重新创建它们时,仍然没有。有人可以看看这个教程并告诉我,如果我疯了,或者他们会遇到类似的结果。我已将其缩小为两种可能性,本教程末尾提供的项目以不同方式创建,代理或出口以仅适用于项目的方式连接,或者项目是使用早期版本的iPhone开发工具包,可与当前版本的XCode配合使用,但在创建新项目时则无效。然后我再次对此嗤之以鼻,所以我会抛出我正在使用的代码并希望最好。

ViewController.h档案:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;

@end

ViewController.m档案:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;

- (void)centerScrollViewContents;
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer;
- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer;
@end

@implementation ViewController

@synthesize scrollView = _scrollView;

@synthesize imageView = _imageView;

- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.imageView.frame;

    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
        contentsFrame.origin.x = 0.0f;
    }

    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }

    self.imageView.frame = contentsFrame;
}

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
    // Get the location within the image view where we tapped
    CGPoint pointInView = [recognizer locationInView:self.imageView];

    // Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
    CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f;
    newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);

    // Figure out the rect we want to zoom to, then zoom to it
    CGSize scrollViewSize = self.scrollView.bounds.size;

    CGFloat w = scrollViewSize.width / newZoomScale;
    CGFloat h = scrollViewSize.height / newZoomScale;
    CGFloat x = pointInView.x - (w / 2.0f);
    CGFloat y = pointInView.y - (h / 2.0f);

    CGRect rectToZoomTo = CGRectMake(x, y, w, h);

    [self.scrollView zoomToRect:rectToZoomTo animated:YES];
}

- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer {
    // Zoom out slightly, capping at the minimum zoom scale specified by the scroll view
    CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f;
    newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
    [self.scrollView setZoomScale:newZoomScale animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Set a nice title
    self.title = @"Image";

    // Set up the image we want to scroll & zoom and add it to the scroll view
    UIImage *image = [UIImage imageNamed:@"photo1.png"];
    self.imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
    [self.scrollView addSubview:self.imageView];

    // Tell the scroll view the size of the contents
    self.scrollView.contentSize = image.size;

    UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
    doubleTapRecognizer.numberOfTapsRequired = 2;
    doubleTapRecognizer.numberOfTouchesRequired = 1;
    [self.scrollView addGestureRecognizer:doubleTapRecognizer];

    UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
    twoFingerTapRecognizer.numberOfTapsRequired = 1;
    twoFingerTapRecognizer.numberOfTouchesRequired = 2;
    [self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // Set up the minimum & maximum zoom scales
    CGRect scrollViewFrame = self.scrollView.frame;
    CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
    CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
    CGFloat minScale = MIN(scaleWidth, scaleHeight);

    self.scrollView.minimumZoomScale = minScale;
    self.scrollView.maximumZoomScale = 1.0f;
    self.scrollView.zoomScale = minScale;

    [self centerScrollViewContents];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{    
    // Return the view that we want to zoom
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    // The scroll view has zoomed, so we need to re-center the contents
    [self centerScrollViewContents];
}

@end

上面的代码不像在教程项目中那样工作。以下UIScrollView委托方法viewForZoomingInScrollView产生此错误:

  

2013-05-14 10:01:18.585 TestScrollView [3809:907]制作

     

5月14日10:01:18 Scott-Laroses-iPhone TestScrollView [3809]:CGAffineTransformInvert:奇异矩阵。

     

5月14日10:01:18 Scott-Laroses-iPhone TestScrollView [3809]:CGAffineTransformInvert:奇异矩阵。

这让我觉得无法正确设置delate,尽管我做的与教程中的完全相同,并且在编程时不起作用。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果分配的值为0,则问题可能来自zoomScaleminimumZoomScale。请确保这两个属性的值永远不为0.

答案 1 :(得分:1)

我明白了!问题在于,自动布局限制会破坏屏幕外的视图。所以我禁用了自动布局,它工作正常。