Iphone iOS多图像分页和缩放问题

Iphone iOS多图像分页和缩放问题,iphone,objective-c,ios,uiscrollview,Iphone,Objective C,Ios,Uiscrollview,有一些线程与此相关,但无法找到一个可靠的解决方案。任何帮助都将不胜感激。如果你给它一张图片,这段代码就非常有用。放大完美。你给它两张图片,它会滚动显示图片,但是当你放大时,它总是会放大第一张图片?不确定为什么viewForZoomingInScrollView方法无法确定您在哪个视图上。代码贴在下面 PagedScrollViewController.h #import <UIKit/UIKit.h> @interface PagedScrollViewController : UI

有一些线程与此相关,但无法找到一个可靠的解决方案。任何帮助都将不胜感激。如果你给它一张图片,这段代码就非常有用。放大完美。你给它两张图片,它会滚动显示图片,但是当你放大时,它总是会放大第一张图片?不确定为什么viewForZoomingInScrollView方法无法确定您在哪个视图上。代码贴在下面

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
#导入
@界面页面CrollViewController:UIViewController
@属性(非原子,强)IBUIScrollView*scrollView;
@属性(非原子,强)IBUIPageControl*pageControl;
@属性(非原子,强)IBUIButton*cancelButtonClicked;
@属性(非原子,强)IBUILabel*头标签;
@属性(非原子,强)字符串*标题;
@属性(非原子,强)IBNSMutableArray*页面图像;
@属性(非原子,强)IBOutlet UITextView*标题;
@属性(弱、非原子)IBUINAVIGATIONBAR*导航条;
@属性(非原子,强)NSMUTABLEARRY*CAPTIONARY;
-(iAction)取消按钮勾选:(id)发送方;
@结束
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
@界面页面ScrollViewController()
@属性(非原子,强)NSMutableArray*页面视图;
-(无效)加载可视页面;
-(无效)加载页:(NSInteger)页;
-(作废)清洗页:(NSInteger)页;
@结束
@实现PagedScrollViewController
@综合滚动视图=_滚动视图;
@综合页面控制=_页面控制;
@合成页面图像=_页面图像;
@综合页面浏览量=_页面浏览量;
@合成cancelButtonClicked=\u cancelButtonClicked;
@综合人头税=_人头税;
@合成人头数=_人头数;
@综合字幕=_字幕;
@合成Captiorary=\u Captiorary;
-(无效)加载可视页面{
//首先,确定当前可见的页面
CGFloat pageWidth=self.scrollView.frame.size.width;
NSInteger页面=(NSInteger)楼层((self.scrollView.contentOffset.x*2.0f+pageWidth)
/(页面宽度*2.0f));
//更新页面控件
self.pageControl.currentPage=page;
NSObject*captionObject=[self.captionaray objectAtIndex:page];
NSString*captionString=[NSString stringWithFormat:@“%@,[captionObject
valueForKey:@“标题”]];
self.caption.text=字幕字符串;
//确定要加载的页面
NSInteger firstPage=第1页;
NSInteger lastPage=第+1页;
//清除第一页之前的任何内容

对于(NSInteger i=0;i这就是我发现的工作原理。支持分页和缩放多个图像。尽情享受吧

#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
#定义缩放标签的视图(1)
@SVViewController的实现
-(无效)viewDidLoad{
[超级视图下载];
UIScrollView*mainScrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
mainScrollView.PaginEnabled=是;
mainScrollView.showsHorizontalScrollIndicator=否;
mainScrollView.showsVerticalScrollIndicator=否;
CGRect innerScrollFrame=mainScrollView.bounds;
对于(NSInteger i=0;i<3;i++){
UIImageView*imageForZooming=[[UIImageView alloc]initWithImage:[UIImageImageName:
[NSString stringWithFormat:@“第%d页,i+1]];
imageForZooming.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=否;
pageScrollView.showsVerticalScrollIndicator=否;
[页面滚动视图添加子视图:用于缩放的图像];
[主滚动视图添加子视图:页面滚动视图];
如果(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*)视图用于缩放CrollView:(UICrollView*)滚动视图{
返回[scrollView viewWithTag:VIEW_FOR_ZOOM_TAG];
}
-(整数)支持的接口方向{
返回UIInterfaceOrientationMaskPortrait;
} 
-(BOOL)应该自动旋转{
返回否;
}
@结束

这就是我发现的工作原理。支持多幅图像的分页和缩放。尽情享受吧

#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
#定义缩放标签的视图(1)
@SVViewController的实现
-(无效)viewDidLoad{
[超级视图下载];
UIScrollView*mainScrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
mainScrollView.PaginEnabled=是;
mainScrollView.showsHorizontalScrollIndicator=否;
mainScrollView.showsVerticalScrollIndicator=否;
CGRect innerScrollFrame=mainScrollView.bounds;
对于(NSInteger i=0;i<3;i++){
UIImageView*imageForZooming=[[UIImageView alloc]initWithImage:[UIImageImageName:
[NSString stringWithFormat:@“第%d页,i+1]];
imageForZooming.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=否;
pageScrollView.showsVerticalScrollIndicator=否;
[页面滚动视图添加子视图:用于缩放的图像];
[主滚动视图添加子视图:页面滚动视图];
如果(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*)视图用于缩放缩略视图:(UIScroll)