如何缩放UIScrollView的内容以适应

时间:2011-09-19 02:37:59

标签: iphone xcode uiscrollview zoom

我在iPhone上的UIScrollView是480x230,首次显示时内容为972x230。我需要提供一些功能,当用户双击UIScrollView时,内容会按比例缩放以适应480x230 UIScrollView。当再次双击它时,它应缩小回原始大小。

最好的方法是什么?我已经摸不着头几个小时,并认为这样可行......

[bodyClockScrollView zoomToRect:bodyClockScrollView.frame animated:YES];

但似乎没有任何事情发生。

谢谢,任何帮助我指向正确方向的人都将不胜感激。

2 个答案:

答案 0 :(得分:2)

您需要确保您设置为scrollview的委托的类实现此方法:

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
return someView;

}

答案 1 :(得分:1)

使用CGAffineTransform我找到了一种方法来做我想要的......

    //bodyCleckScrollView contains a UIView name bodyClock
    //get the scale factor required to fit bodyClock inside the iPhone window and resize it...
    CGFloat scale = 480/bodyClockScrollView.contentSize.width;  
    bodyClock.transform = CGAffineTransformScale(bodyClock.transform, scale, scale);
    //move bodyClock to the bottom of bodyScrollView.
    bodyClock.transform = CGAffineTransformTranslate(bodyClock.transform, 0.0,bodyClockScrollView.frame.size.height-bodyClock.frame.size.height);
    //scoll bodyScrollView so that bodyClock is centered in the window 
    CGPoint offsetPoint = CGPointMake(bodyClock.frame.origin.x, 0.0);
    [bodyClockScrollView setContentOffset:offsetPoint animated:YES];   

这很有效,当我想将其缩小到默认大小和位置时,您只需调用...

    bodyClock.transform = CGAffineTransformIdentity;