【问题标题】:Sample code for ios6 based scrollview with pagination and zooming features for an array of images基于 ios6 的滚动视图的示例代码,具有图像数组的分页和缩放功能
【发布时间】:2013-06-23 01:27:19
【问题描述】:

谁能帮我提供在分页滚动视图中嵌入可缩放滚动视图的示例代码,以便每个页面都可以单独缩放和平移?

导航结构也类似于标签栏控制器 --> 带有按钮的导航控制器(按下按钮) --> 视图控制器,其中图像的水平滚动视图必须通过分页和缩放来实现。

这是我遵循的教程的链接:How To Use UIScrollView to Scroll and Zoom Content

我实现的代码如下:

#import "ViewCorpBrochureController.h"

@interface ViewCorpBrochureController ()
@property (nonatomic, strong) NSArray *pageImages;
@property (nonatomic, strong) NSMutableArray *pageViews;

@property (nonatomic, strong) UIImageView *imageView;

- (void)loadVisiblePages;
- (void)loadPage:(NSInteger)page;
- (void)purgePage:(NSInteger)page;


- (void)centerScrollViewContents;
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer;
- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer;



@end

@implementation ViewCorpBrochureController

@synthesize scrollView = _scrollView;
@synthesize pageControl = _pageControl;

@synthesize pageImages = _pageImages;
@synthesize pageViews = _pageViews;


@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];
}


#pragma mark -

- (void)loadVisiblePages {
    // First, determine which page is currently visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));

    // Update the page control
    self.pageControl.currentPage = page;

    // Work out which pages we want to load
    NSInteger firstPage = page - 1;
    NSInteger lastPage = page + 1;

    // Purge anything before the first page
    for (NSInteger i=0; i<firstPage; i++) {
        [self purgePage:i];
    }
    for (NSInteger i=firstPage; i<=lastPage; i++) {
        [self loadPage:i];
    }
    for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
        [self purgePage:i];
    }
}

- (void)loadPage:(NSInteger)page {
    if (page < 0 || page >= self.pageImages.count) {
        // If it's outside the range of what we have to display, then do nothing
        return;
    }

    // Load an individual page, first seeing if we've already loaded it
    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView == [NSNull null]) {
        CGRect frame = self.scrollView.bounds;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0.0f;

        UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
        newPageView.contentMode = UIViewContentModeScaleAspectFit;
        newPageView.frame = frame;
        [self.scrollView addSubview:newPageView];
        [self.pageViews replaceObjectAtIndex:page withObject:newPageView];
    }
}

- (void)purgePage:(NSInteger)page {
    if (page < 0 || page >= self.pageImages.count) {
        // If it's outside the range of what we have to display, then do nothing
        return;
    }

    // Remove a page from the scroll view and reset the container array
    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView != [NSNull null]) {
        [pageView removeFromSuperview];
        [self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
    }
}


#pragma mark -



- (void)viewDidLoad {
    [super viewDidLoad];




   self.title = @"CorporateBrochure";


    self.pageImages = [NSArray arrayWithObjects:
                       [UIImage imageNamed:@"CB1.jpg"],
                       [UIImage imageNamed:@"CB2.jpg"],
                       [UIImage imageNamed:@"CB3.jpg"],
                       [UIImage imageNamed:@"CB4.jpg"],
                       [UIImage imageNamed:@"CB5.jpg"],
                       [UIImage imageNamed:@"CB6.jpg"],
                       nil];

    NSInteger pageCount = self.pageImages.count;

    // Set up the page control
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = pageCount;

    // Set up the array to hold the views for each page
    self.pageViews = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < pageCount; ++i) {
        [self.pageViews addObject:[NSNull null]];
    }


    /* If I use this the subview appears below the image
     UIImage *image = self.imageView.image;
    //UIImage *image = [UIImage imageNamed:@"CB2.jpg"];
    self.imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
    [self.scrollView addSubview:self.imageView];
    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)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    CGSize pagesScrollViewSize = self.scrollView.frame.size;
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);

    [self loadVisiblePages];

}


- (void)viewDidUnload {
    [super viewDidUnload];

    self.scrollView = nil;
    self.pageControl = nil;
    self.pageImages = nil;
    self.pageViews = nil;
}



- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    // Return the view that we want to zoom
    return self.imageView;
}




- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationMaskAll);
}



#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // Load the pages which are now on screen
    [self loadVisiblePages];
}



- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    // The scroll view has zoomed, so you need to re-center the contents
    [self centerScrollViewContents];
}

@end

【问题讨论】:

  • 尝试解释您遇到的问题,而不仅仅是“尽快”寻求解决方案
  • 您好,damienc 实际上我正在尝试实现 ray wenderlich 教程的滚动、平移和缩放滚动视图示例,并在将其应用于图像数组时感到震惊。你能帮帮我吗?
  • 关于 TBlue,我一直在努力实现带分页的可缩放滚动视图,但是是的,我刚刚加入了 stackoverflow,所以无法正确发布我的代码,如果你需要我,肯定会发布代码到。
  • 请张贴,并解释您遇到的问题。请将此作为对您问题的编辑。也可能是教程的链接。
  • 非常感谢先生 :) 我将根据您的宝贵建议粘贴链接和代码。

标签: ios uiviewcontroller uiscrollview uistoryboard uipageviewcontroller


【解决方案1】:

Ray Wenderlich 的教程有什么具体问题吗?

Apple 的文档中有一个很好的示例来说明如何实现分页滚动视图,请参阅Photo Scroller sample code here。我建议从那里开始。

从上面的链接:

“PhotoScroller”演示了嵌入式 UIScrollViews 和 CATiledLayer 为显示和创建丰富的用户体验 可以单独平移和缩放的分页照片。

注意:如果你的图片不是太大,你可以不使用CATiledLayer。

编辑 1: 请参阅 my answer 至 this question 了解如何修改 Apple 的照片滚动代码以使 UIPageViewController 成为您自己的视图控制器的子视图(而不是 rootViewController) .

编辑 2: 请参阅 github 上的 sample project。

【讨论】:

  • 您能否在页面视图的代码 sn-p 之后再次查看该代码我正在尝试做的是在现有页面视图中添加子视图,但问题是我无法加载图像到目前为止,全屏视图显示在第一张图片下方。
  • 我也尝试实现苹果照片滚动器示例,但由于我们在项目中使用的复杂导航架构,我们遇到了根视图控制器错误。我希望能够清楚地提出我的问题。
  • @AlamHammad 查看我的编辑,希望这可以帮助您克服根视图控制器错误。
  • appdelegate.m中的didFinishLaunchingWithOptions应该怎么做?
  • 我已经实施了您在回复中建议的任何内容,但我的视图显示为空白(黑色):(。如果可能的话,您能否与我分享任何相同的工作示例。
猜你喜欢
  • 2016-10-01
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 2014-01-07
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多