我正在构建一个由不同视图组成的应用程序,这些视图彼此密切相关。 到目前为止,我只有一个UIViewController来控制这些不同的视图。 例如,视图1和2共享相同的背景,视图1和2之间的过渡是自定义动画。
我的问题是视图1和2都有一个UIScrollView。 我的UIViewController是他们的委托,我可以使用以下scrollViewDidScroll来区分两个scrollviews:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.tag == 1) //handle a else if (scrollView.tag == 2) //handle b else if (scrollView.tag == 3) //handle c }滚动视图1发生了很多,滚动视图2发生了不同的事情,代码将变得非常混乱。 理想情况下,我想在一个单独的文件中定义滚动滚动等会发生什么。但是我不希望有另一个UIViewController,因为转换变得更加困难。 我没有NavBar或ToolBar,所以UINavigationController和UITabBarController在我的情况下都不会很好用。
我该怎么办?
我在这里发了一个类似的问题。
I'm building an app which consists of different views which are closely related to each other. So far, I only have one UIViewController which controls these different views. View 1 and 2 share the same background, for instance, and the transition between view 1 and 2 is a custom animation.
My problem is that both view 1 and 2 have an UIScrollView. My UIViewController is their delegate and I could have the following scrollViewDidScroll to distinguish between the two scrollviews:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.tag == 1) //handle a else if (scrollView.tag == 2) //handle b else if (scrollView.tag == 3) //handle c }As a lot happens with scrollView 1 and different things happen with scrollView2, the code will become really messy. Ideally, I'd like to define in a separate file what happens if scrollView1 is scrolled etc.. Yet I don't want to have another UIViewController as then transitions become more difficult. I don't have a NavBar or ToolBar, so neither UINavigationController nor UITabBarController would work very well in my case.
What should I do?
I posted a similar question here.
最满意答案
如果您不想要两个视图控制器,只需为每个滚动视图创建一个单独的委托。 使它成为一个符合UIScrollViewDelegate的NSObject ,并在滚动视图的同时创建它。
似乎要组合您寻求的结果:一个视图控制器,但封装的滚动视图代码。
If you don't want two view controllers, just create a separate delegate for each scroll view. Make it an NSObject which conforms to UIScrollViewDelegate and create it at the same time as the scroll view.
Seems to combine the results you seek: one view controller, but encapsulated scroll view code.
更多推荐
发布评论