视差 UIScrollView – 在一个 scrollViewDidScroll 方法中处理两个视图? [英] Parallax UIScrollView – handling two views in one scrollViewDidScroll method?

查看:68
本文介绍了视差 UIScrollView – 在一个 scrollViewDidScroll 方法中处理两个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个 UIScrollView 实例(cardScrollViewtileScrollView)创建一个与 iOS7 应用切换器非常相似的基本视差效果.我以不同的速度将一个并排滚动,如下所示:

I’m creating a basic parallax effect much like the iOS7 app switcher, using two UIScrollView instances (cardScrollView and tileScrollView). I scroll one alongside the other, at a different rate, like so:

if ([scrollView isEqual:self.tileScrollView]) {
    [self.cardScrollView setContentOffset:CGPointMake((self.tileScrollView.contentOffset.x + 110) * TILE_CARD_DELTA,
                                                      self.cardScrollView.contentOffset.y)];
}

这在滚动 tileScrollView 时工作正常.但是,我希望反向工作也一样,这意味着我可以滚动 cardScrollView 并让 tileScrollView 相应地移动.我遇到的问题是调用 setContentOffset 实际上会导致 cardScrollView 调用 scrollViewDidScroll 本身,这意味着它们不断尝试将彼此设置为同时,各种地狱都挣脱了.

This works fine when scrolling tileScrollView. However, I’d like the same to work in reverse, meaning I can scroll cardScrollView and have tileScrollView move accordingly. The issue I’m having is that calling setContentOffset actually causes cardScrollView to call scrollViewDidScroll itself, meaning they’re continually trying to set each other at the same time, and all kinds of hell break loose.

基本上,这里的问题是两个 scrollView 实例都依赖于相同的 scrollViewDidScroll,因此我无法区分其中的两个.

Basically, the issue here is that both scrollView instances are relying on the same scrollViewDidScroll, and so I can’t differentiate between the two of them in there.

我该如何解决这个问题?

How can I get around this one?

推荐答案

您正在此方法中获得参考并按要求工作:

You are getting reference of both in this method and work as per requirement :

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

 {
   if (scrollView == self.tileScrollView) {
      // do something
   }
   else {
      // do something
   }
  }

这篇关于视差 UIScrollView – 在一个 scrollViewDidScroll 方法中处理两个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆