使用 UISwitch 将两个滚动视图锁定在一起

发布于 2024-11-05 17:10:38 字数 1433 浏览 5 评论 0原文

我正在寻求一点帮助。我通常很擅长找到我需要的东西,但这很棘手。

这是我的测试应用程序的场景:我有 2 个 1024x85 的滚动视图,它们仅水平移动。我在它们下面还有一个 UISwitch。在滚动视图上方,我有两个标签,用于显示每个滚动视图移动时的内容偏移量(这样我就可以看到发生了什么)。

我想要做什么:用户将每个视图从一侧滑动到另一侧后,我想使用 UISwitch 将这些滚动视图锁定在一起,无论它们位于何处。

这是更新后的代码:

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

 CGPoint p = scrollOne.contentOffset;
 CGPoint r = scrollTwo.contentOffset;

 // Print the contentOffset labels
 scrollOneLabel.text = [NSString stringWithFormat:@"%.2f", p.x];
 scrollTwoLabel.text = [NSString stringWithFormat:@"%.2f", r.x];


// If lock is on, the distance between offsets is locked (but limited to max and min)
if (lockSwitch.on) {

    NSInteger offset = scrollOne.contentOffset.x - scrollTwo.contentOffset.x;

    if (scrollView == scrollOne) {

        NSInteger maxOffset = scrollTwo.contentSize.width - scrollTwo.frame.size.width;
        [scrollTwo setContentOffset: CGPointMake(MIN(MAX(0.0,scrollOne.contentOffset.x - offset), maxOffset), 0.0)];

    } else if (scrollView == scrollTwo) {
        NSInteger maxOffset = scrollOne.contentSize.width - scrollOne.frame.size.width;
        [scrollOne setContentOffset: CGPointMake(MIN(MAX(0.0,scrollTwo.contentOffset.x + offset), maxOffset), 0.0)];
    }
}

// If the lock is not on, both move independently
}

它将滚动视图锁定在一起,但是一旦我移动其中一个滚动视图,第二个滚动视图就会跳转到与第一个滚动视图相同的内容偏移量。我试图将它们锁定在当时的位置,而不是当用户触摸其中一个时将它们排列起来。

预先感谢您的帮助。

I'm looking for a little help. I'm usually pretty good at finding what I need, but this one's tricky.

Here's the scene for my test app: I have 2 scrollviews that are 1024x85 and they only move horizontal. I also have a UISwitch below them. Above the scrollviews I have two labels that display the content offset of each scrollview as it moves (so I can see what's going on).

What I want to do: After the user slides each of the views side to side I would like to use the UISwitch to lock those scrollviews together wherever they may be sitting.

This is the updated code:

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

 CGPoint p = scrollOne.contentOffset;
 CGPoint r = scrollTwo.contentOffset;

 // Print the contentOffset labels
 scrollOneLabel.text = [NSString stringWithFormat:@"%.2f", p.x];
 scrollTwoLabel.text = [NSString stringWithFormat:@"%.2f", r.x];


// If lock is on, the distance between offsets is locked (but limited to max and min)
if (lockSwitch.on) {

    NSInteger offset = scrollOne.contentOffset.x - scrollTwo.contentOffset.x;

    if (scrollView == scrollOne) {

        NSInteger maxOffset = scrollTwo.contentSize.width - scrollTwo.frame.size.width;
        [scrollTwo setContentOffset: CGPointMake(MIN(MAX(0.0,scrollOne.contentOffset.x - offset), maxOffset), 0.0)];

    } else if (scrollView == scrollTwo) {
        NSInteger maxOffset = scrollOne.contentSize.width - scrollOne.frame.size.width;
        [scrollOne setContentOffset: CGPointMake(MIN(MAX(0.0,scrollTwo.contentOffset.x + offset), maxOffset), 0.0)];
    }
}

// If the lock is not on, both move independently
}

It locks the scrollviews together, but as soon as I move one of the scrollviews the second scrollview jumps to the same content offset as the first. I'm trying to lock them where they are at that moment instead of lining them up when the user touches one of them.

Thank is in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2024-11-12 17:10:38

所以基本上你想保持两者之间的偏移量不变?在这种情况下,您需要一个变量,我们将其称为 offset,即 scrollOne.contentOffset.x -scrollTwo.contentOffset.x ,锁定它们后只需对委托做出反应方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // If lock is on, the distance between offsets is locked (but limited to max and min)
    if (lockSwitch.on) {
         if (scrollView == scrollOne) {
             NSInteger maxOffset = scrollTwo.contentSize.width - scrollTwo.frame.size.width;
             [scrollTwo setContentOffset: CGPointMake(MIN(MAX(0.0,scrollOne.contentOffset.x - offset), maxOffset), 0.0)];
         } else if (scrollView == scrollTwo) {
             NSInteger maxOffset = scrollOne.contentSize.width - scrollOne.frame.size.width;
             [scrollOne setContentOffset: CGPointMake(MIN(MAX(0.0,scrollTwo.contentOffset.x + offset), maxOffset), 0.0)];
         }
    }

    // If the lock is not on, both move independently
}

编辑:您必须在 UISwitch 更改状态时设置偏移量,而不是在每个滚动事件上设置偏移量。使用 [addTarget:selector:forControlEvents:] 方法将类似的内容绑定到您的 UISwitch。请记住,偏移量必须是类中的全局变量。

- (void)lockChanged:(id)sender {
    UISwitch *lock = sender;
    if (lock.on) {
        offset = (int)(scrollOne.contentOffset.x - scrollTwo.contentOffset.x);
    } else {
        offset = 0;
    }
}

So basically you want to keep the offset between the two constant? In that case you need a variable, let's call it offset, which would be scrollOne.contentOffset.x - scrollTwo.contentOffset.x and after locking them just react to the delegate method

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // If lock is on, the distance between offsets is locked (but limited to max and min)
    if (lockSwitch.on) {
         if (scrollView == scrollOne) {
             NSInteger maxOffset = scrollTwo.contentSize.width - scrollTwo.frame.size.width;
             [scrollTwo setContentOffset: CGPointMake(MIN(MAX(0.0,scrollOne.contentOffset.x - offset), maxOffset), 0.0)];
         } else if (scrollView == scrollTwo) {
             NSInteger maxOffset = scrollOne.contentSize.width - scrollOne.frame.size.width;
             [scrollOne setContentOffset: CGPointMake(MIN(MAX(0.0,scrollTwo.contentOffset.x + offset), maxOffset), 0.0)];
         }
    }

    // If the lock is not on, both move independently
}

EDIT: You have to set the offset when the UISwitch changes state, not on every scrolling event. Have something like this tied to your UISwitch with the [addTarget:selector:forControlEvents:] method. Remember, the offset has to be a global variable in your class.

- (void)lockChanged:(id)sender {
    UISwitch *lock = sender;
    if (lock.on) {
        offset = (int)(scrollOne.contentOffset.x - scrollTwo.contentOffset.x);
    } else {
        offset = 0;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文