Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4601336
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:48:38+00:00 2026-05-21T23:48:38+00:00

I’m looking for a little help. I’m usually pretty good at finding what I

  • 0

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 1024×85 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:48:38+00:00Added an answer on May 21, 2026 at 11:48 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.