嘿,我正在關注這個tutorial,目前正在進行第一步,但我正在經歷一些奇怪的行爲。本教程旨在爲圖像創建滾動視圖,並以展示如何使用頁面控制器創建分頁的水平ScrollView結束。我跟着第一部分,事情似乎工作正常。然而,當我運行代碼時,它沒有正確執行,經過大約一個小時的調試後,我下載了最終的項目,在手機上運行它以檢查它是否正常工作(它是),然後將代碼複製到我所需的文件中項目。我的項目仍然無法運行。所以我認爲這一定是我的故事板在項目設置中配置的方式。然後我打開了這個工作示例,清除了他們的故事板,並創建了視圖,並以我完成的項目中完成的方式和順序創建了我的自我。巴姆!它仍然有效,當我刪除故事板中的所有視圖並以完全相同的方式重新創建它們時,仍然沒有任何結果。有人能看看這個教程,並告訴我,如果我瘋了,或者他們遇到類似的結果。我將其縮小爲兩種可能性,本教程末尾提供的項目創建方式不同,代表或插座以僅與該項目協同工作的方式聯結,或者該項目使用早期版本的iPhone開發工具包可與當前版本的XCode配合使用,但不能在創建新項目時使用。然後我再次相當新,所以我會拋出我使用的代碼並希望最好。IOS ScrollView問題
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] 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.
這讓我想起了啦免費統計沒有正確設置,儘管我這樣做完全一樣的教程和程序時沒有工作。有任何想法嗎?
你忘了吐出代碼;) – danypata 2013-05-14 14:41:10
我開始在我的電腦上寫下這個問題,不需要考慮代碼,所以我發佈它打開我的Mac並完成它。沒想到有人會注意到,但男孩是我錯了! @danypata – ScottOBot 2013-05-14 14:48:32
可能重複[ios::CGAffineTransformInvert:奇異矩陣](http://stackoverflow.com/questions/17499645/ios-error-cgaffinetransforminvert-singular-matrix) –
kenorb
2016-07-21 10:47:50