Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 05-08-2011, 11:44 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 2
Wudstock is on a distinguished road
Default 'Lock' two scrollViews together with a UISwitch

Hi All,

I have a tough one that I can't figure out. I am trying to "Lock" two scrollviews where they are sitting using a UISwitch, so when you move one the other moves without snapping to the other's contentOffset.

I have a test app going with 2 scrollviews that are 1024x85 each and only scroll horizontal. Below those a UISwitch, and above the scrollviews are two labels that displays the contentOffset of both scrollviews as I move them (so I can see what's going on).

The code I have so far works and "Locks" the scrollviews together BUT, once I start sliding one of the scrollviews the other one snaps to the same contentOffset as the one i'm sliding. This displays the labels as the same number.

An example of what i'm looking for is: I slide 'scrollOne' so the label reads 1000. I slide 'scrollTwo' so its label reads 500. I lock them by turning the switch ON. Then I slide 'scrollOne' so its label reads 500. 'scrollTwo' should move as I slide 'scrollOne'. The label for 'scrollTwo' should now read zero.

Here's the code I have so far:

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

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

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

       // Flip the switch to lock the scrollviews together
  if (lockSwitch.on) {

     if (scrollView == scrollOne) {
        scrollTwo.contentOffset = CGPointMake(scrollOne.contentOffset.x, 0);
  }
  else if (scrollView == scrollTwo) {
       scrollOne.contentOffset = CGPointMake(scrollTwo.contentOffset.x, 0);
      }
   }
}
Any Ideas?
Thanks in advance for any help
Wudstock
Wudstock is offline   Reply With Quote
Old 05-08-2011, 01:21 PM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

you need to store the difference between contentOffset.x of the 2 uiscrollview when switch change to on, and then apply the difference offset each time you scroll the uiscrollview
__________________
dany_dev is offline   Reply With Quote
Old 05-11-2011, 08:55 PM   #3 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 2
Wudstock is on a distinguished road
Default Lock two scrollviews

Thanks Dany,

I figured it out. For anyone else wondering how to do this, here's the code:

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


- (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.contentOffset = 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.contentOffset = CGPointMake(MIN(MAX(0.0,scrollTwo.contentOffset.x + offset), maxOffset), 0.0);
        }
    }
    
        // If the lock is not on, both move independently
}
Wudstock
Wudstock is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 382
18 members and 364 guests
AyClass, baja_yu, checkright, dre, epaga, fvisticot, givensur, jbro, jPuzzle, Meoz, momolgtm, Newbie123, Punkjumper, revg, sacha1996, skrew88, tomtom100
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,642
Threads: 94,110
Posts: 402,858
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pinacate
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:35 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0