开发者

Locking two scrollviews together with UISwitch

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 setCo开发者_Python百科ntentOffset: 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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜