iOS多图像分页和缩放问题 [英] iOS Multiple Images Paging and Zooming Issues

查看:57
本文介绍了iOS多图像分页和缩放问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些与此问题相关的线程,但是找不到可靠的解决方案.任何帮助,将不胜感激.如果给它一张图像,此代码将非常有用.完美放大.您给它提供了两个图像,它会滚动,从而使图像变好,但是当您放大图像时,它始终会放大到第一个图像吗?不知道为什么viewForZoomingInScrollView方法不能弄清楚您所在的视图.代码在下面

There are a few threads out there related to this issues, but could not find a solid solution. Any help would be appreciated. This code works great if you give it one image. Zooms in perfect. You give it two images it scrolls threws the images fine but when you go to zoom in it always zooms into the first image? Not sure why the viewForZoomingInScrollView method doesn't figure out which view you are on. Code posted below

PagedScrollViewController.h

PagedScrollViewController.h

#import <UIKit/UIKit.h>
@interface PagedScrollViewController : UIViewController <UIScrollViewDelegate>
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) IBOutlet UIButton *cancelButtonClicked;
@property (nonatomic, strong) IBOutlet UILabel *headerTitle;
@property (nonatomic, strong) IBOutlet NSString *headerTitleS;
@property (nonatomic, strong) IBOutlet NSMutableArray *pageImages;
@property (nonatomic, strong) IBOutlet UITextView *caption;
@property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar;
@property (nonatomic, strong) NSMutableArray *captionArray;

-(IBAction)cancelButtonClicked:(id)sender;

@end

PagedScrollViewController.m

PagedScrollViewController.m

@interface PagedScrollViewController ()
@property (nonatomic, strong) NSMutableArray *pageViews;

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

@implementation PagedScrollViewController
@synthesize scrollView = _scrollView;
@synthesize pageControl = _pageControl;
@synthesize pageImages = _pageImages;
@synthesize pageViews = _pageViews;
@synthesize cancelButtonClicked = _cancelButtonClicked;
@synthesize headerTitle = _headerTitle;
@synthesize headerTitleS = _headerTitleS;
@synthesize caption = _caption;
@synthesize captionArray = _captionArray;

- (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;

    NSObject *captionObject = [self.captionArray objectAtIndex:page];
    NSString *captionString = [NSString stringWithFormat:@"%@", [captionObject 
    valueForKey:@"caption"]]; 
    self.caption.text = captionString;

    // 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 = CGRectMake((page*320), 0, 310, 320);
        self.scrollView.maximumZoomScale = 1.3;
        self.scrollView.contentSize = CGSizeMake(320*[self.pageImages count],389);
        self.scrollView.frame = CGRectMake(320*(self.pageControl.currentPage), 44, 310, 
        370);
        [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]];
     }
 }

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
      self.scrollView.contentSize = CGSizeMake(320*[self.pageImages count],389);
     return [self.scrollView.subviews objectAtIndex:self.pageControl.currentPage];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollView.delegate = self;
    self.headerTitle.text = self.headerTitleS;
    // Set up the image we want to scroll & zoom and add it to the scroll view
    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]];
    }
}

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
   // Load the initial set of pages that are on screen
   [self loadVisiblePages];
}

- (void)viewDidUnload {
   [super viewDidUnload];
   self.scrollView = nil;
   self.pageControl = nil;
   self.pageImages = nil;
   self.pageViews = nil;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  [self loadVisiblePages];
}

-(IBAction)cancelButtonClicked:(id)sender{
   [self dismissViewControllerAnimated:YES completion:nil];
}

@end

推荐答案

这是我发现可以使用的方法.支持具有分页和缩放功能的多个图像.享受吧!

This is what I found to work. Supports multiple images with paging and zooming. Enjoy!

#define VIEW_FOR_ZOOM_TAG (1)

@implementation SVViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    mainScrollView.pagingEnabled = YES;
    mainScrollView.showsHorizontalScrollIndicator = NO;
    mainScrollView.showsVerticalScrollIndicator = NO;

    CGRect innerScrollFrame = mainScrollView.bounds;

    for (NSInteger i = 0; i < 3; i++) {
    UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:[UIImage imageNamed:  
    [NSString stringWithFormat:@"page%d", i + 1]]];
    imageForZooming.tag = VIEW_FOR_ZOOM_TAG;

    UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
    pageScrollView.minimumZoomScale = 1.0f;
    pageScrollView.maximumZoomScale = 2.0f;
    pageScrollView.zoomScale = 1.0f;
    pageScrollView.contentSize = imageForZooming.bounds.size;
    pageScrollView.delegate = self;
    pageScrollView.showsHorizontalScrollIndicator = NO;
    pageScrollView.showsVerticalScrollIndicator = NO;
    [pageScrollView addSubview:imageForZooming];

    [mainScrollView addSubview:pageScrollView];

    if (i < 2) {
        innerScrollFrame.origin.x += innerScrollFrame.size.width;
    }
    }

    mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + 
    innerScrollFrame.size.width, mainScrollView.bounds.size.height);

    [self.view addSubview:mainScrollView];
   }

 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  return [scrollView viewWithTag:VIEW_FOR_ZOOM_TAG];
}

 - (NSUInteger)supportedInterfaceOrientations {
 return UIInterfaceOrientationMaskPortrait;
} 

- (BOOL)shouldAutorotate {
 return NO;
}

@end

这篇关于iOS多图像分页和缩放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆