sinh99
sinh99

Reputation: 3979

Two scroll view work Simultaneous with one touch

I am working on application in it I have to work with two scroll view Simultaneous in one touch. It means if I scroll one scroll view at same time another scroll view must scroll with it.

If this is possible then how can it be done?

Upvotes: 6

Views: 5911

Answers (4)

Ketan Patel
Ketan Patel

Reputation: 560

  • (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

if( scrollView == scrollViewA ) // change offset of B else // change offset of A

Upvotes: 0

user956224
user956224

Reputation:

Understand this code; may be it helps you.


(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardshow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardhide:) name:UIKeyboardDidHideNotification object:nil];
    myscrollview.contentSize=CGSizeMake(560, 420);
    showkeyboard=NO;
}

(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] 
    removeObserver:self];
}


(void) keyboardshow : (NSNotification *) notification
{
    if (showkeyboard)
    {
        return;
    }

    NSDictionary *info=[notification userInfo];

    NSValue *avalue=[info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [avalue CGRectValue].size;
    offset = myscrollview.contentOffset;
    CGRect viewFrame = myscrollview.frame;
    viewFrame.size.height -= keyboardSize.height;
    myscrollview.frame = viewFrame;
    CGRect textFieldRect =[mytext3 frame];
    textFieldRect.origin.y +=10;
    [myscrollview scrollRectToVisible:textFieldRect animated:YES];
    showkeyboard =YES;
}

(void) keyboardhide : (NSNotification *) notification
{
    if(!showkeyboard)
    {
        return;
    }

    myscrollview.frame =CGRectMake(0, 0, 320, 460);
    myscrollview.contentOffset=offset;
    showkeyboard=NO;

}

Upvotes: -1

TigerCoding
TigerCoding

Reputation: 8720

Implement the UIScrollViewDelegate protocol in the view controller containing both scroll views. In the:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

delegate method, get the content offset:

CGPoint offset = [scrollViewA contentOffset]; // or scrollViewB

Then set the other control with:

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

You can determine which one to change by comparing in the delegate method above:

if( scrollView == scrollViewA ) // change offset of B
else // change offset of A

Upvotes: 10

Jab
Jab

Reputation: 27485

Don't have to read

Generally (at least from what I know) it's bad "style" to have 2 UIScrollView/UITableVIew's in each other because it causes the UI to be hard to interact with. But I think if you have a valid enough use/reason for doing it then I'll show you a way to get it done.

CODE!

If it was me then I'd just override the UIScrollView's touchesMoved method and scroll the other UIScrollView that way.

Within scrollView_1

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {

    UITouch *touch = [ touches anyObject];
    CGPoint newLocation = [touch locationInView: [touch view]];
    CGPoint oldLocation = [touch previousLocationInView:touch.view];
    CGPoint translation = CGPointMake(newLocation.x - oldLocation.x, newLocation.y - oldLocation.y);
    scrollView_2.contentOffset = CGPointMake(scrollView_2.contentOffset.x + translation.x, scrollView_2.contentOffset.y + translation.y)

}

Hope this helps

Upvotes: 3

Related Questions