xcode 将 scrollViewDidScroll 与多个 UIScrollView 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1482889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:52:10  来源:igfitidea点击:

Using scrollViewDidScroll with multiple UIScrollViews

cocoacocoa-touchxcodeuiscrollview

提问by crgt

I have two horizontally scrolling UIScrollViews stacked vertically on top of each other within one ViewController. Each UIScrollView takes up half the screen. I am trying to independently track the position of both UIScrollViews.

我有两个水平滚动的 UIScrollViews 在一个 ViewController 中垂直堆叠在一起。每个 UIScrollView 占据屏幕的一半。我试图独立跟踪两个 UIScrollViews 的位置。

I have successfully used this to track the position of the top UIScrollView:

我已经成功地使用它来跟踪顶部 UIScrollView 的位置:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView1 {

int Position = (scrollView1.contentOffset.x);

    if (Position == 0) {
    NSLog(@"Play A"); 
    }

    else if (Position == 280) {
    NSLog(@"Play B"); 
    }

//etc.
}

I would like to track the position of the bottom UIScrollView as well.

我也想跟踪底部 UIScrollView 的位置。

When I try to use this:

当我尝试使用它时:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView2 {

int Position2 = (scrollView2.contentOffset.x);

if (Position2 == 0) {
NSLog(@"Play A2"); 
}

else if (Position == 280) {
NSLog(@"Play B2"); 
}

//etc.
}

I get an error that says "Redefinition of FirstViewConroller scrollViewDidScroll".

我收到一条错误消息,显示“重新定义 FirstViewConroller scrollViewDidScroll”。

So, determined to press on, I tried a rather hackish solution and attempted to use this instead:

因此,决定继续前进,我尝试了一个相当黑客的解决方案并尝试使用它:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView2

{

int Position2 = (scrollView2.contentOffset.x);

if (Position2 == 0) {
NSLog(@"Play A2"); 
}

else if (Position2 == 280) {
NSLog(@"Play B2"); 
}

//etc.
}

The problem with this is that it triggers bothmethods when I move either of the UIScrollViews. For example -- if I move the bottom UIScrollView 280 pixels (one image) to the right, the output I get in the console is:

问题在于,当我移动 UIScrollView 中的任何一个时,它会触发这两种方法。例如——如果我将底部 UIScrollView 向右移动 280 像素(一张图像),我在控制台中得到的输出是:

PlayB
PlayB2

播放
B播放B2

And if I move the top UIScrollView three images to the right the output I get is:

如果我将顶部 UIScrollView 三个图像向右移动,我得到的输出是:

PlayC
PlayC2

播放
C播放C2

Which doesn't make any sense to me.

这对我来说没有任何意义。

I think I may be bumping up against my own incomplete understanding of how delegates work. Within the viewDidLoad method I am setting both:

我想我可能会遇到我自己对代表如何工作的不完整理解。在 viewDidLoad 方法中,我同时设置:

scrollView1.delegate = self;
scrollView2.delegate = self;

Perhaps this is part of the problem? Maybe I am causing trouble by declaring that both scrollViews have the same delegate? Just not sure.

也许这是问题的一部分?也许我通过声明两个 scrollView 具有相同的委托而造成了麻烦?只是不确定。

I've also tried combining all of my conditional statements into one method -- but I only want to track the position of the bottom UIScrollView when it moves and the position of the top UIScrollView when it moves, and putting the logic all in one method reports both positions when either one moves and this is not what I'm looking for.

我还尝试将所有条件语句组合到一个方法中——但我只想跟踪底部 UIScrollView 移动时的位置和顶部 UIScrollView 移动时的位置,并将逻辑全部放在一个方法中当其中一个移动时报告两个位置,这不是我要找的。

Any help is appreciated. Would love to solve this particular problem, but would also really love to understand any bigger issues with how I'm approaching this. Still new at this. Keep thinking I'm getting the hang of it and then I run into something that stops me in my tracks..

任何帮助表示赞赏。很想解决这个特定的问题,但也很想了解我如何解决这个问题的任何更大的问题。在这方面还是新的。一直想着我已经掌握了它,然后我遇到了一些阻止我前进的事情。

回答by kperryua

This is exactly what the UIScrollViewparameter is for. Create connections to scrollView1 and scrollView2 in IB and then do this:

这正是UIScrollView参数的用途。在 IB 中创建与 scrollView1 和 scrollView2 的连接,然后执行以下操作:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView1 == scrollView) {
        // Do stuff with scrollView1
    } else if (scrollView2 == scrollView) {
        // Do stuff with scrollView2
    }
}

回答by NSResponder

There's no reason why the same object can't be the delegate of several different scrollviews. The reason that -scrollViewDidScroll: passes you the scrollview in question is so that you know whichscrollview did scroll.

没有理由为什么同一个对象不能成为几个不同滚动视图的委托。-scrollViewDidScroll: 传递给您有问题的滚动视图的原因是让您知道哪个滚动视图确实滚动了。

This is the general pattern for delegate messages.

这是委托消息的一般模式。