ios – 如何在uipageviewcontroller中放大或缩小?

ios – 如何在uipageviewcontroller中放大或缩小?,第1张

概述我有一个uipageviewcontroller并在uipageviewcontroller中设置了一个名为contentviewcontroller的子视图控制器. thePageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePag 我有一个uipagevIEwcontroller并在uipagevIEwcontroller中设置了一个名为contentvIEwcontroller的子视图控制器.

thePageVIEwController = [[UIPageVIEwController alloc] initWithTransitionStyle:UIPageVIEwControllerTransitionStylePageCurl navigationorIEntation: UIPageVIEwControllerNavigationorIEntationHorizontal options:nil];    thePageVIEwController.delegate = self;    thePageVIEwController.dataSource = self;    thePageVIEwController.vIEw.autoresizingMask = UIVIEwautoresizingFlexibleWIDth | UIVIEwautoresizingFlexibleHeight;    contentVIEwController = [[HBdocumentChildVIEwController alloc] initWithpdf:pdfdocument];    contentVIEwController.page = [modelArray objectAtIndex:0];    NSArray *vIEwControllers = [NSArray arrayWithObject:contentVIEwController];    [thePageVIEwController setVIEwControllers:vIEwControllers direction:UIPageVIEwControllerNavigationDirectionForward animated:NO completion:nil];    [self addChildVIEwController:thePageVIEwController];    [self.vIEw addSubvIEw:thePageVIEwController.vIEw];    thePageVIEwController.vIEw.frame = CGRectMake(0,self.vIEw.frame.size.wIDth,self.vIEw.frame.size.height);    [thePageVIEwController dIDMovetoParentVIEwController:self];    self.vIEw.gestureRecognizers = thePageVIEwController.gestureRecognizers;    self.vIEw.backgroundcolor = [UIcolor underPageBackgroundcolor];

我已经完成了借助滚动视图类(pdfScrollVIEw)将pdf文件加载到uipagevIEwcontroller中的页面.followig是uipagevIEwcontroller的内容类并初始化uiscrollvIEw类.

#import "HBdocumentChildVIEwController.h"@interface HBdocumentChildVIEwController ()<uiwebviewdelegate>{    int currentPage;    Nsstring*localPath;}@end@implementation HBdocumentChildVIEwController#pragma mark - VIEw lifecycle- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    // Create our pdfScrollVIEw and add it to the vIEw controller.    CGpdfpageRef pdfpage = CGpdfdocumentGetPage(thepdf,[_page intValue]);    pdfScrollVIEw = [[pdfScrollVIEw alloc] initWithFrame:self.vIEw.frame];    [pdfScrollVIEw setpdfpage:pdfpage];    [self.vIEw addSubvIEw:pdfScrollVIEw];    self.vIEw.backgroundcolor = [UIcolor whitecolor];    self.vIEw.autoresizingMask = UIVIEwautoresizingFlexibleWIDth | UIVIEwautoresizingFlexibleHeight;    pdfScrollVIEw.autoresizingMask = UIVIEwautoresizingFlexibleWIDth | UIVIEwautoresizingFlexibleHeight;}

以下是uiscrollvIEw类,

/*     file: pdfScrollVIEw.m Abstract: UIScrollVIEw subclass that handles the user input to zoom the pdf page.  This class handles swapPing the TiledpdfVIEws when the zoom level changes.  Version: 2 */#import "pdfScrollVIEw.h"#import "TiledpdfVIEw.h"#import <QuartzCore/QuartzCore.h>@interface pdfScrollVIEw ()// A low resolution image of the pdf page that is displayed until the TiledpdfVIEw renders its content.@property (nonatomic,weak) UIImageVIEw *backgroundImageVIEw;// The TiledpdfVIEw that is currently front most.@property (nonatomic,weak) TiledpdfVIEw *tiledpdfVIEw;// The old TiledpdfVIEw that we draw on top of when the zooming stops.@property (nonatomic,weak) TiledpdfVIEw *oldTiledpdfVIEw;@end@implementation pdfScrollVIEw{    CGpdfpageRef _pdfpage;    // Current pdf zoom scale.    CGfloat _pdfScale;    CGfloat _pdfScaleH;}@synthesize backgroundImageVIEw=_backgroundImageVIEw,tiledpdfVIEw=_tiledpdfVIEw,oldTiledpdfVIEw=_oldTiledpdfVIEw;- (ID)initWithCoder:(NSCoder *)coder{    self = [super initWithCoder:coder];    if (self) {        self.decelerationRate = UIScrollVIEwDecelerationRateFast;        self.delegate = self;    }    return self;}- (voID)setpdfpage:(CGpdfpageRef)pdfpage;{    CGpdfpageRetain(pdfpage);    CGpdfpageRelease(_pdfpage);    _pdfpage = pdfpage;    // Determine the size of the pdf page.    CGRect pageRect = CGpdfpageGetBoxRect(_pdfpage,kCGpdfMediaBox);    float actualHeight = pageRect.size.height;    float actualWIDth = pageRect.size.wIDth;    float imgRatio = actualWIDth/actualHeight;    float maxRatio = 768.0/911.0;    if(imgRatio!=maxRatio){        if(imgRatio < maxRatio){            imgRatio = 911.0 / actualHeight;            actualWIDth = imgRatio * actualWIDth;            actualHeight = 911.0;        }        else{            imgRatio = 768.0 / actualWIDth;            actualHeight = imgRatio * actualHeight;            actualWIDth = 768.0;        }    }    pageRect.size = CGSizeMake(actualWIDth,actualHeight);    UIGraphicsBeginImageContext(pageRect.size);    /*     Create a low resolution image representation of the pdf page to display before the TiledpdfVIEw renders its content.     */    UIGraphicsBeginImageContext(pageRect.size);    CGContextRef context = UIGraphicsGetCurrentContext();    // First fill the background with white.    CGContextSetRGBFillcolor(context,1.0,1.0);    CGContextFillRect(context,pageRect);    CGContextSaveGState(context);    // Flip the context so that the pdf page is rendered right sIDe up.    CGContextTranslateCTM(context,0.0,pageRect.size.height);    CGContextScaleCTM(context,-1.0);    // Scale the context so that the pdf page is rendered at the correct size for the zoom level.    CGContextScaleCTM(context,imgRatio,imgRatio);    CGContextDrawpdfpage(context,_pdfpage);    CGContextRestoreGState(context);    UIImage *backgroundImage = UIGraphicsGetimageFromCurrentimageContext();    UIGraphicsEndImageContext();    if (self.backgroundImageVIEw != nil) {        [self.backgroundImageVIEw removeFromSupervIEw];    }    UIImageVIEw *backgroundImageVIEw = [[UIImageVIEw alloc] initWithImage:backgroundImage];    backgroundImageVIEw.frame = pageRect;    backgroundImageVIEw.contentMode = UIVIEwContentModeScaleAspectFit;    [self addSubvIEw:backgroundImageVIEw];    [self sendSubvIEwToBack:backgroundImageVIEw];    self.backgroundImageVIEw = backgroundImageVIEw;    // Create the TiledpdfVIEw based on the size of the pdf page and scale it to fit the vIEw.    TiledpdfVIEw *tiledpdfVIEw = [[TiledpdfVIEw alloc] initWithFrame:pageRect scale:imgRatio];    [tiledpdfVIEw setPage:_pdfpage];    [self addSubvIEw:tiledpdfVIEw];    self.tiledpdfVIEw = tiledpdfVIEw;}- (voID)dealloc{    // Clean up.    CGpdfpageRelease(_pdfpage);}#pragma mark -#pragma mark OverrIDe layoutSubvIEws to center content// Use layoutSubvIEws to center the pdf page in the vIEw.- (voID)layoutSubvIEws {    [super layoutSubvIEws];    // Center the image as it becomes smaller than the size of the screen.    CGSize boundsSize = self.bounds.size;    CGRect frametoCenter = self.tiledpdfVIEw.frame;    // Center horizontally.    if (frametoCenter.size.wIDth < boundsSize.wIDth)        frametoCenter.origin.x = (boundsSize.wIDth - frametoCenter.size.wIDth) / 2;    else        frametoCenter.origin.x = 0;    // Center vertically.    if (frametoCenter.size.height < boundsSize.height)        frametoCenter.origin.y = (boundsSize.height - frametoCenter.size.height) / 2;    else        frametoCenter.origin.y = 0;    self.tiledpdfVIEw.frame = frametoCenter;    self.backgroundImageVIEw.frame = frametoCenter;    /*     To handle the interaction between CATiledLayer and high resolution screens,set the tiling vIEw's contentScaleFactor to 1.0.     If this step were omitted,the content scale factor would be 2.0 on high resolution screens,which would cause the CATiledLayer to ask for tiles of the wrong scale.     */    self.tiledpdfVIEw.contentScaleFactor = 1.0;}#pragma mark -#pragma mark UIScrollVIEw delegate methods/* A UIScrollVIEw delegate callback,called when the user starts zooming. Return the current TiledpdfVIEw. */- (UIVIEw *)vIEwForZoomingInScrollVIEw:(UIScrollVIEw *)scrollVIEw{    return self.tiledpdfVIEw;}/* A UIScrollVIEw delegate callback,called when the user begins zooming. When the user begins zooming,remove the old TiledpdfVIEw and set the current TiledpdfVIEw to be the old vIEw so we can create a new TiledpdfVIEw when the zooming ends. */- (voID)scrollVIEwWillBeginZooming:(UIScrollVIEw *)scrollVIEw withVIEw:(UIVIEw *)vIEw{    // Remove back tiled vIEw.    [self.oldTiledpdfVIEw removeFromSupervIEw];    // Set the current TiledpdfVIEw to be the old vIEw.    self.oldTiledpdfVIEw = self.tiledpdfVIEw;    [self addSubvIEw:self.oldTiledpdfVIEw];}/* A UIScrollVIEw delegate callback,called when the user stops zooming. When the user stops zooming,create a new TiledpdfVIEw based on the new zoom level and draw it on top of the old TiledpdfVIEw. */- (voID)scrollVIEwDIDEndZooming:(UIScrollVIEw *)scrollVIEw withVIEw:(UIVIEw *)vIEw atScale:(float)scale{    // Set the new scale factor for the TiledpdfVIEw.    _pdfScale *= scale;    // Calculate the new frame for the new TiledpdfVIEw.    CGRect pageRect = CGpdfpageGetBoxRect(_pdfpage,kCGpdfMediaBox);    pageRect.size = CGSizeMake(pageRect.size.wIDth*_pdfScale,pageRect.size.height*_pdfScale);    // Create a new TiledpdfVIEw based on new frame and scaling.    TiledpdfVIEw *tiledpdfVIEw = [[TiledpdfVIEw alloc] initWithFrame:pageRect scale:_pdfScale];    [tiledpdfVIEw setPage:_pdfpage];    // Add the new TiledpdfVIEw to the pdfScrollVIEw.    [self addSubvIEw:tiledpdfVIEw];    self.tiledpdfVIEw = tiledpdfVIEw;}@end

页面渲染工作正常,但我没有在页面显示视图中进行放大/缩小.uiscrollvIEw委托方法在uipagevIEwcontroller中不起作用.请帮我找到解决此问题的方法.

解决方法 试试这个

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];[thePageVIEwController addGestureRecognizer:pinchRecognizer];pinchRecognizer.delegate=self;

方法是

-(voID)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer{    CGfloat scale = pinchRecognizer.scale;    thePageVIEwController.transform = CGAffinetransformScale(thePageVIEwController.transform,scale,scale);    pinchRecognizer.scale = 1.0;}
总结

以上是内存溢出为你收集整理的ios – 如何在uipageviewcontroller中放大或缩小?全部内容,希望文章能够帮你解决ios – 如何在uipageviewcontroller中放大或缩小?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1076022.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存