gpt4 book ai didi

IOS ScrollView 问题

转载 作者:行者123 更新时间:2023-11-28 22:34:19 24 4
gpt4 key购买 nike

嘿,我正在关注这个 tutorial目前处于第一步,但遇到了一些奇怪的行为。本教程介绍如何为图像创建 ScrollView ,最后展示如何使用页面 Controller 创建分页水平 ScrollView 。我遵循了第一部分,一切似乎都很好。然而,当我运行代码时它没有正常执行,经过大约一个小时的调试我下载了最终项目,在我的手机上运行它以检查它是否正常工作(它是)然后将代码复制到我的所需文件中项目。我的项目仍然无法运行。所以我认为这一定是我的 Storyboard在项目设置中的配置方式。然后我打开工作示例,清除了他们的 Storyboard并创建了 View ,并按照我在我的项目中所做的完全相同的方式和顺序创建了自己的 View 。砰!它仍然有效,当我删除 Storyboard中的所有 View 并以完全相同的方式重新创建它们时,仍然没有。有人可以查看本教程并告诉我我是否会发疯或者他们会遇到类似的结果。我已将其缩小为两种可能性,教程末尾提供的项目创建方式不同,代表或 socket 以仅适用于该项目的方式连接,或者该项目是使用早期版本的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 委托(delegate)方法 viewForZoomingInScrollView 会产生此错误:

2013-05-14 10:01:18.585 TestScrollView[3809:907] Made it

May 14 10:01:18 Scott-Laroses-iPhone TestScrollView[3809] : CGAffineTransformInvert: singular matrix.

May 14 10:01:18 Scott-Laroses-iPhone TestScrollView[3809] : CGAffineTransformInvert: singular matrix.

这让我觉得 delate 没有正确设置,尽管我在教程中以编程方式完全相同,但它不起作用。有什么想法吗?

最佳答案

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

关于IOS ScrollView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16546154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com