Pregunta

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

¿Fue útil?

Solución

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.

Otros consejos

Here's a Swift version of Andrei's answer. I am also exiting early if the user is swiping up (scrolling down), because I only wanted to show the zoom effect when they were pulling down on the table view.

// Exit early if swiping up (scrolling down)
if scrollView.contentOffset.y > 0 { return }

// this is just a demo method on how to compute the scale factor based on the current contentOffset

var scale = 1.0 + fabs(scrollView.contentOffset.y)  / scrollView.frame.size.height

//Cap the scaling between zero and 1
    scale = max(0.0, scale)

// Set the scale to the imageView
self.imageView.transform = CGAffineTransformMakeScale(scale, scale)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top