How to have non scrolling content in a UIScrollView

南笙酒味 提交于 2019-12-08 08:33:06

问题


I need something like this

+ scrollview
|
|__ moving content
|
|__ non moving content

The tricky part is that I'm modifying an existing piece of code where I can't use the scrollview parent to set the "non moving content" as it's child. The non moving content must be a scrollview child. I was thinking of attaching it to the background view but I don't know how to access the UIScrollview's background view.


回答1:


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.

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

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

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

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.



来源:https://stackoverflow.com/questions/8891940/how-to-have-non-scrolling-content-in-a-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!