Two scroll view functions at the same time with one touch

I am working on an application in it. I need to work with two scroll. One touch at a time. This means that if I scroll one kind of scroll, at the same time the scroll will scroll along with it.

If possible, then how can this be done?

+6
source share
4 answers

Implement the UIScrollViewDelegate protocol in a view controller that contains both types of scrolling. IN:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 

delegate method, get content offset:

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

Then set another control with

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

You can determine which one should be changed by comparing the delegate method above:

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

No need to read

Usually (at least from what I know), it is a bad β€œstyle” to have 2 UIScrollView / UITableVIew in each other, because it makes it difficult to interact with the user interface. But I think that if you have a reasonable enough use / reason for this, I will show you a way to do it.

CODE!

If it were me, I would simply override the UIScrollView touchesMoved method and scroll through the other UIScrollView in this 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

+3
source

Understand this code; maybe this will help 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; } 
0
source
  • (void) setContentOffset: (CGPoint) contentOffset animated: (BOOL) animated

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

0
source

Source: https://habr.com/ru/post/902617/


All Articles