且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在一个UIScrollView非滚动内容

更新时间:2023-11-18 13:14:22

我相信推荐的方式做到这一点是重写layoutSubviews在自定义UIScrollView的子类。这是在2010年的WWDC会议上覆盖(IIRC),你应该能够得到,如果你是一个开发者计划的成员。

I believe the "recommended" way to do this is to override layoutSubviews in a custom UIScrollView subclass. This was covered (IIRC) in a 2010 WWDC session you ought to be able to get if you are a developer program member.

假设你不能子类UIScrollView的,你可以这样做以下(在修改现有的code给你的限制)。

Assuming you can't subclass UIScrollView (given your restrictions on modifying existing code), you can do something like the following.

让staticView是非移动内容您想保留固定的:

Let "staticView" be the "non moving content" you wish to keep fixed:

UIView *staticView;

创建伊娃(可能在您的视图控制器),以保持其初始中心:

Create an ivar (probably in your view controller) to hold its initial center:

CGPoint staticViewDefaultCenter = [staticView center]; // Say, in viewDidLoad

请您的视图控制器滚动视图的委托,如果尚未,然后实现类似如下:

Make your view controller the scroll view's delegate if it isn't already, then implement something like the following:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint contentOffset = [scrollView contentOffset];
    CGPoint newCenter = CGPointMake(staticViewDefaultCenter.x + contentOffset.x,
                                    staticViewDefaultCenter.y + contentOffset.y);
    [staticView setCenter:newCenter];
 }

这将涵盖滚动视图的简单情况。处理可缩放视图变得有点棘手,将部分取决于你如何执行你的viewForZoomingInScrollView:方法,但遵循的类似方法(​​这一次是在scrollViewDidZoom:当然)。

This will cover the simple case of a scrollable view. Handling a zoomable view gets a little trickier and will depend in part on how you have implemented your viewForZoomingInScrollView: method, but follows an analogous procedure (this time in scrollViewDidZoom: of course).

我没有通过有你staticView变换的含义想 - 你可能必须做在这种情况下一些操作太

I haven't thought through the implications of having a transform on your staticView - you may have to do some manipulations in that case too.