如何缩放 UIScrollView 的内容以适应

发布于 2024-12-05 09:15:20 字数 337 浏览 0 评论 0原文

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

最好的方法是什么?我已经摸索了几个小时,并认为这会起作用......

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

但似乎什么也没有发生。

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

My UIScrollView on the iPhone is 480x230 and the content is 972x230 when first displayed. I need to provide functionality where when the user double taps the UIScrollView the contents zoom to fit with the 480x230 UIScrollView proportionally. When the double tap it again it should zoom back out to it's original size.

What is the best way to do this? I have been fumbling for several hours with this and thought that this would work...

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

But nothing seems to happen.

Thanks, any help pointing me in the right direction would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-12-12 09:15:20

您需要确保您设置为滚动视图委托的类实现了此方法:

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

}

You need to make sure the class you've set up as the scrollview's delegate implements this method:

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

}

白云悠悠 2024-12-12 09:15:20

使用 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;

Using CGAffineTransform I figured out a way to do what I want...

    //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];   

This works great and when I want to zoom it back out to the default size and position you simply call...

    bodyClock.transform = CGAffineTransformIdentity;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文