警告: これは私の最初の iPhone アプリケーションなので、ここでは多くのカーゴ カルト プログラミングが行われています。
これを修正するために、グーグルで検索し、stackoverflow を検索し、本を読み、コードを何度も変更しましたが、できません。Jonathan Zdziarski による「iPhone SDK アプリケーション開発」にある PageScrollView クラスを使用しています。これは、一度に 3 つをメモリにロードして、5 つ (またはそれ以上) の画像のビューを提供するクラスです。これらの PageScrollViews のうち 3 つを一度に画面上に重ねて表示したい。横方向にスクロールする 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) ];
しかし、これらの数字は私には正しくないようです。それぞれ高さが 160 ピクセルで (3x160=480) の 3 つの画像があるため、y 値はそれぞれ 0,160、320 にすべきだと思いますが、y 値を 0,160、320 に設定すると、一番上の画像が一番上になります。画面では、中央の画像は下の画像があるべき場所であり、下の画像は画面からはみ出しています。しかし、y 値を 0、80、および 160 に設定すると、画像は正しく表示されますが、スクロール領域は機能します (スクロール領域またはタッチ領域と呼ばれるものが画像の上に設定されているという直感があります)。
誰にもアイデアはありますか?