iOS Animate Zoom To Fit Width

Q:

Hi, I’ve implemented a zooming behavior similar to how the Photos app work on iOS in PDFNet, however one thing that’s bothering me is that there is no way to animate the zooming out in PDFNet on iOS when user is double tapping.

The zooming in part is easy using

-(BOOL)SmartZoomX:(double)in_x y:(double)in_y animated:(BOOL)animated;

and

  • (void)zoomToRect:(CGRect)rect animated:(BOOL)animated;

However when zooming out, I use:

-(void)SetPageViewMode:(TrnPageViewMode)mode;

-(void)SetPageRefViewMode:(TrnPageViewMode)mode;

to reset the zoom to the page mode that the user initially selected (e_trn_fit_page for presentations and e_trn_fit_width for documents). There is no animated zooming out method and I wouldn’t know what zoom factor to use since it also depends on the orientation.

I’ve tried putting the view mode methods in a UIView animation block:

  • (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;

But the page just slides in from top left corner instead of animating out.

Is there some way I can animate the zooming out using the page view mode methods?

A:

To animate zooming out you’ll need to use the method

  • (void)zoomToRect:(CGRect)rect animated:(BOOL)animated;

If you want to zoom out to exactly fit width or fit page, you need to use other PDFViewCtrl methods to calculate what that rectangle is, and then animate to it. The last call to zoomToRect:animate: in handleDoubleTap: in tool.m (and also here below) shows how to zoom back out to a fit width zoom level.

double zoomBy = ([m_pdfViewCtrl GetCanvasWidth])/m_pdfViewCtrl.bounds.size.width;

[self.pdfViewCtrl zoomToRect:CGRectMake((self.pdfViewCtrl.bounds.size.width/2-[self.pdfViewCtrl GetCanvasWidth]/2), ([self.pdfViewCtrl GetVScrollPos]+self.pdfViewCtrl.bounds.size.height/2), self.pdfViewCtrl.bounds.size.widthzoomBy, self.pdfViewCtrl.bounds.size.heightzoomBy) animated:YES];