Search code examples
iphoneiosipad

content offset changes unexpectedly while zooming programmatically in uiscrollview


I am having 1.0 - 4.0 zoomscale when I zoom after scale 3.7, content offset changes unexpectedly to last visible rect (lower - right).

How to fix this?


Solution

  • Shift your origin of scroll view according to the size of view after zooming.

     - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    
         CGFloat vPadding;
         CGFloat hPadding;
         if ((480-view.frame.size.height)/2 > 0) {
              vPadding=(480-view.frame.size.height)/2;
         }
         else{
              vPadding=0;
         }
         if ((320-view.frame.size.width)/2 > 0) {
              hPadding=(320-view.frame.size.width)/2;
         }
         else{
              hPadding=0;
         }
         yourView.frame = CGRectMake(hPadding, vPadding, view.frame.size.width, view.frame.size.height);
    
    }
    

    OR change scroll view centre accordingly.