Search code examples
iphoneuiscrollviewuiimageviewuiscrollviewdelegate

How to zoom in a uiimageview as I scroll down the table?


Hello everyone I am trying to zoom in a out a uiimageview using animation as i am scrolling down a uiscrollview. Please note its not about making a uiimageview zoom in and out in a uiscrollview.

I am able to detect how much i am scrolling down :

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
}

I need to zoom out the image depending( the scroll factor) how much i am scrolling down. Any idea how this can be done?

enter image description here


Solution

  • I suggesting changing the transform property of the image you are trying to scale

    - (void)scrollViewDidScroll:(UIScrollView*)scrollView{
    
        // this is just a demo method on how to compute the scale factor based on the current contentOffset
        float scale = 1.0f + fabsf(scrollView.contentOffset.y)  / scrollView.frame.size.height; 
    
        //Cap the scaling between zero and 1
        scale = MAX(0.0f, scale);
    
        // Set the scale to the imageView
        imageView.transform = CGAffineTransformMakeScale(scale, scale);    
    }
    

    If you also need the scrollview to go to the top one the imageview has been zoomed out, then you will need to adjust the frame of the scrollview and imageview.

    Maybe you can tell us more about your desired effect.