当我向下滚动表格时,如何放大uiimageview?

时间:2013-08-02 11:26:00

标签: iphone uiscrollview uiimageview uiscrollviewdelegate

大家好,我正在尝试使用动画放大uiimageview,因为我向下滚动uiscrollview。请注意,它不是uiimageview中的uiscrollview放大和缩小。

我能够检测到我向下滚动了多少:

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

我需要根据(滚动因子)缩小图像我向下滚动多少。知道如何做到这一点?

enter image description here

2 个答案:

答案 0 :(得分:10)

我建议更改您要缩放的图片的transform属性

- (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);    
}

如果您还需要滚动视图转到顶部,图像视图已缩小,则需要调整滚动视图和图像视图的框架。

也许你可以告诉我们更多你想要的效果。

答案 1 :(得分:1)

这是安德烈答案的斯威夫特版本。如果用户向上滑动(向下滚动),我也会提前退出,因为我只想在拉下桌面视图时显示缩放效果。

// 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)