警告:这是我的第一个 iPhone 应用程序,所以这里有很多货物崇拜程序。
我用谷歌搜索,搜索stackoverflow,阅读书籍,并一遍又一遍地更改代码以试图解决这个问题,但我做不到。我正在使用 Jonathan Zdziarski 在“iPhone SDK 应用程序开发”中找到的 PageScrollView 类。它是一个提供 5 个(或更多)图像视图的类,一次将 3 个加载到内存中。我希望屏幕上的 3 个这样的 PageScrollViews 一次堆叠在一起。当手机处于纵向位置时,顶行 5 张图像水平滚动,中间一行图像水平滚动,底行图像水平滚动,我在横向模式下完全切换视图.
PageScrollView 由我修改,任何被注释掉的东西都曾被取消注释以试图使这项工作。我试图尽可能多地注释掉以隔离问题:
#import "PageScrollView.h"
@implementation PageScrollView
-(id)initWithFrame:(CGRect)frame {
self = [ super initWithFrame: frame ];
if (self != nil) {
_pages = nil;
_zeroPage = 0;
_pageRegion = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
//_controlRegion = CGRectMake(frame.origin.x,frame.size.height/2, frame.size.width, 0);
self.delegate = nil;
scrollView = [ [ UIScrollView alloc ] initWithFrame: _pageRegion ];
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
[ scrollView setContentOffset: CGPointMake(frame.origin.x, frame.origin.y) ];
[ scrollView setContentSize: CGSizeMake(_pageRegion.size.width,_pageRegion.size.height) ];
[ scrollView setContentInset : UIEdgeInsetsMake(0.0,80.0,0,0) ];
[ self addSubview: scrollView ];
//pageControl = [ [ UIPageControl alloc ] initWithFrame: _controlRegion ];
//[ pageControl addTarget: self action: @selector(pageControlDidChange:) forControlEvents: UIControlEventValueChanged ];
//[ self addSubview: pageControl ];
}
return self;
}
-(void)setPages:(NSMutableArray *)pages {
if (pages != nil) {
for(int i=0;i<[_pages count];i++) {
[ [ _pages objectAtIndex: i ] removeFromSuperview ];
}
}
_pages = pages;
//scrollView.contentOffset = CGPointMake(0.0, 0.0);
scrollView.contentOffset = CGPointMake(0.0, _pageRegion.origin.y);
if ([ _pages count] < 3) {
scrollView.contentSize = CGSizeMake(_pageRegion.size.width * [ _pages count ], _pageRegion.size.height);
} else {
scrollView.contentSize = CGSizeMake(_pageRegion.size.width * 3, _pageRegion.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
}
pageControl.numberOfPages = [ _pages count ];
pageControl.currentPage = 0;
[ self layoutViews ];
}
- (void)layoutViews {
if ([ _pages count ] <= 3) {
for(int i=0;i<[ _pages count];i++) {
UIView *page = [ _pages objectAtIndex: i ];
//CGRect bounds = page.bounds;
//CGRect frame = CGRectMake(_pageRegion.size.width * i, 0.0, _pageRegion.size.width, _pageRegion.size.height);
//CGRect frame = CGRectMake(_pageRegion.size.width * i, _pageRegion.origin.y+_pageRegion.size.height , _pageRegion.size.width, _pageRegion.size.height);
//page.frame = frame;
//page.bounds = bounds;
[ scrollView addSubview: page ];
}
return;
}
/* For more than 3 views, add them all hidden, layout according to page */
for(int i=0;i<[ _pages count];i++) {
UIView *page = [ _pages objectAtIndex: i ];
//CGRect bounds = page.bounds;
//CGRect frame = CGRectMake(0.0, 0.0, _pageRegion.size.width, _pageRegion.size.height);
//CGRect frame = CGRectMake(0.0, _pageRegion.origin.y+_pageRegion.size.height, _pageRegion.size.width, _pageRegion.size.height);
//page.frame = frame;
//page.bounds = bounds;
page.hidden = YES;
[ scrollView addSubview: page ];
}
[ self layoutScroller ];
}
- (void)layoutScroller {
UIView *page;
CGRect bounds, frame;
int pageNum = [ self getCurrentPage ];
if ([ _pages count ] <= 3)
return;
NSLog(@"Laying out scroller for page %d\n", pageNum);
/* Left boundary */
if (pageNum == 0) {
for(int i=0;i<3;i++) {
page = [ _pages objectAtIndex: i ];
bounds = page.bounds;
//frame = CGRectMake(_pageRegion.size.width * i, 0.0, _pageRegion.size.width, _pageRegion.size.height);
frame = CGRectMake(_pageRegion.size.width * i, 0.0, _pageRegion.size.width, _pageRegion.size.height);
NSLog(@"\tOffset for Page %d = %f\n", i, frame.origin.x);
page.frame = frame;
page.bounds = bounds;
page.hidden = NO;
}
page = [ _pages objectAtIndex: 3 ];
page.hidden = YES;
_zeroPage = 0;
}
/* Right boundary */
else if (pageNum == [ _pages count ] -1) {
for(int i=pageNum-2;i<=pageNum;i++) {
page = [ _pages objectAtIndex: i ];
bounds = page.bounds;
frame = CGRectMake(_pageRegion.size.width * (2-(pageNum-i)), 0.0, _pageRegion.size.width, _pageRegion.size.height);
NSLog(@"\tOffset for Page %d = %f\n", i, frame.origin.x);
page.frame = frame;
page.bounds = bounds;
page.hidden = NO;
}
page = [ _pages objectAtIndex: [ _pages count ]-3 ];
page.hidden = YES;
_zeroPage = pageNum - 2;
}
/* All middle pages */
else {
for(int i=pageNum-1; i<=pageNum+1; i++) {
page = [ _pages objectAtIndex: i ];
bounds = page.bounds;
frame = CGRectMake(_pageRegion.size.width * (i-(pageNum-1)), 0.0, _pageRegion.size.width, _pageRegion.size.height);
NSLog(@"\tOffset for Page %d = %f\n", i, frame.origin.x);
page.frame = frame;
page.bounds = bounds;
page.hidden = NO;
}
for(int i=0; i< [ _pages count ]; i++) {
if (i < pageNum-1 || i > pageNum + 1) {
page = [ _pages objectAtIndex: i ];
page.hidden = YES;
}
}
//scrollView.contentOffset = CGPointMake(_pageRegion.size.width, 0.0);
scrollView.contentOffset = CGPointMake(_pageRegion.size.width, 0.0);
_zeroPage = pageNum-1;
}
}
-(id)getDelegate {
return _delegate;
}
- (void)setDelegate:(id)delegate {
_delegate = delegate;
}
-(BOOL)getShowsPageControl {
return _showsPageControl;
}
-(void)setShowsPageControl:(BOOL)showsPageControl {
_showsPageControl = showsPageControl;
if (_showsPageControl == NO) {
//_pageRegion = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
pageControl.hidden = YES;
scrollView.frame = _pageRegion;
} else {
//_pageRegion = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height - 60.0);
//_pageRegion = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
pageControl.hidden = NO;
scrollView.frame = _pageRegion;
}
}
-(NSMutableArray *)getPages {
return _pages;
}
-(void)setCurrentPage:(int)page {
// [ scrollView setContentOffset: CGPointMake(0.0, 0.0) ];
[ scrollView setContentOffset: CGPointMake(0.0,_pageRegion.origin.y) ];
_zeroPage = page;
[ self layoutScroller ];
pageControl.currentPage = page;
}
-(int)getCurrentPage {
return (int) (scrollView.contentOffset.x / _pageRegion.size.width) + _zeroPage;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControl.currentPage = self.currentPage;
[ self layoutScroller ];
[ self notifyPageChange ];
}
-(void) pageControlDidChange: (id)sender
{
UIPageControl *control = (UIPageControl *) sender;
if (control == pageControl) {
//[ scrollView setContentOffset: CGPointMake(_pageRegion.size.width * (control.currentPage - _zeroPage), 0.0) animated: YES ];
[ scrollView setContentOffset: CGPointMake(_pageRegion.size.width * (control.currentPage - _zeroPage), 0.0) animated: YES ];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[ self layoutScroller ];
[ self notifyPageChange ];
}
-(void) notifyPageChange {
if (self.delegate != nil) {
// if ([ _delegate conformsToProtocol:@protocol(PageScrollViewDelegate) ]) {
if ([ _delegate respondsToSelector:@selector(pageScrollViewDidChangeCurrentPage:currentPage:) ]) {
[ self.delegate pageScrollViewDidChangeCurrentPage: (PageScrollView *)self currentPage: self.currentPage ];
}
// }
}
}
@end
这是我的 ViewController 类中的 loadview 方法:
- (void)loadView {
[ super loadView ];
/* Load demo images for pages */
topPages = [ [ NSMutableArray alloc ] init ];
for(int i = 0; i < 5; i++) {
UIImage *image = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: [ NSURL URLWithString: [ NSString stringWithFormat: @"http://www.zdziarski.com/demo/%d.png", i+1 ] ] ] ];
UIImageView *page = [ [ UIImageView alloc ] initWithFrame: CGRectMake(0,0,320,160) ] ;
page.image = image;
//page.bounds = CGRectMake(0.0, 0.0, 320, 160);
[ topPages addObject: page ];
}
CGRect viewBounds = [ [ UIScreen mainScreen ] applicationFrame ];
viewBounds.origin.y = 0.0;
portraitTopView = [ [ PageScrollView alloc ] initWithFrame: CGRectMake(0,0,320,160) ];
portraitTopView.pages = topPages;
portraitTopView.delegate = self;
//portraitTopView.showsPageControl = NO;
middlePages = [ [ NSMutableArray alloc ] init ];
for(int i = 0; i < 5; i++) {
UIImage *image = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: [ NSURL URLWithString: [ NSString stringWithFormat: @"http://www.zdziarski.com/demo/%d.png", i+1 ] ] ] ];
UIImageView *page = [ [ UIImageView alloc ] initWithFrame: CGRectMake(0,0,320,160) ] ;
page.image = image;
//page.bounds = CGRectMake(0.0, 0.0, 320, 160);
[ middlePages addObject: page ];
}
portraitMiddleView = [ [ PageScrollView alloc ] initWithFrame: CGRectMake(0,80,320,160) ];
portraitMiddleView.pages = middlePages;
portraitMiddleView.delegate = self;
//portraitMiddleView.showsPageControl = NO;
bottomPages = [ [ NSMutableArray alloc ] init ];
for(int i = 0; i < 5; i++) {
UIImage *image = [ [ UIImage alloc ] initWithData: [ NSData dataWithContentsOfURL: [ NSURL URLWithString: [ NSString stringWithFormat: @"http://www.zdziarski.com/demo/%d.png", i+1 ] ] ] ];
UIImageView *page = [ [ UIImageView alloc ] initWithFrame: CGRectMake(0,0,320,160) ] ;
page.image = image;
//page.bounds = CGRectMake(0.0, 0.0, 320, 160);
[ bottomPages addObject: page ];
}
portraitBottomView = [ [ PageScrollView alloc ] initWithFrame: CGRectMake(0,160,320,160) ];
portraitBottomView.pages = bottomPages;
portraitBottomView.delegate = self;
//portraitBottomView.showsPageControl = NO;
portraitView = [ [ UIView alloc ] initWithFrame: viewBounds ];
[ portraitView addSubview: portraitTopView ];
[ portraitView addSubview: portraitMiddleView ];
[ portraitView addSubview: portraitBottomView ];
landscapeTextView = [ [ UITextView alloc ] initWithFrame: viewBounds ];
landscapeTextView.text = woahDizzy;
self.view = portraitView;
}
我的问题是滚动区域(我触摸的屏幕部分)似乎与图像不匹配。为了让图像正确显示,我正在使用:
portraitTopView = [ [ PageScrollView alloc ] initWithFrame: CGRectMake(0,0,320,160) ];
portraitMiddleView = [ [ PageScrollView alloc ] initWithFrame: CGRectMake(0,80,320,160) ];
portraitBottomView = [ [ PageScrollView alloc ] initWithFrame: CGRectMake(0,160,320,160) ];
但是,这些数字对我来说似乎不正确。我觉得 y 值应该分别为 0,160 和 320,因为我有 3 个图像,每个图像高 160 像素和 (3x160=480),但是当我将 y 值设置为 0,160 和 320 时,顶部图像位于顶部屏幕,中间图像是底部图像应该在的位置,底部图像远离屏幕。但是当我将 y 值设置为 0,80 和 160 时,图像会正确显示,但滚动区域确实有效(我有一种直觉,滚动区域或触摸区域(无论它们被称为什么)都设置在图像上方)。
有没有人有任何想法?