屏幕上一次显示多个PageScrollViews(UIScrollViews)(未嵌套)

时间:2009-06-08 13:03:49

标签: iphone uiscrollview

警告:这是我的第一个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时,图像显示正确,但滚动区域确实有效(我有一种直觉,即滚动区域或触摸区域,无论它们被调用在图像上方)。

有没有人有任何想法?

1 个答案:

答案 0 :(得分:1)

如果您没有尝试手动构建布局,我认为事情可能会轻松得多。看看这个:

创建UIScrollView(viewBase是UIScrollView的一个实例)

int imgWidth = 160;
int numImages = 3;
int scrollWidth  = imgWidth * numImages;

int imgHeight = 160;
int numRows = 3;
int scrollHeight = imgHeight * numRows;

viewBase.contentSize = CGSizeMake(scrollWidth, scrollHeight);
// Set anything else you need on viewbase

然后,当您将元素添加到UIScrollView时,请执行类似的计算。

// Add this View to the far left of the scroller.
webViewOne = [[UIWebView alloc] initWithFrame: CGRectMake(viewBase.frame.origin.x, viewBase.frame.origin.y, viewBase.frame.size.width, viewBase.frame.size.height)];
[viewBase addSubview:webViewOne];
[webViewOne release];

// Add this View tot he right of webViewOne (Note the offset on the X coordinate)
webViewTwo = [[UIWebView alloc] initWithFrame: CGRectMake((viewBase.frame.size.width * 1), viewBase.frame.origin.y, viewBase.frame.size.width, viewBase.frame.size.height)];
[viewBase addSubview:webViewTwo];
[webViewTwo release];

这允许所有大小调整和布局都与底层的UIScrollView相关,而不是试图逐个像素地控制事物。