两个滚动视图工作同时触摸

我正在处理它的应用程序,我必须使用两个滚动视图Simultaneous一触式工作。 这意味着如果我同时滚动一个滚动视图,另一个滚动视图必须滚动。

如果这是可能的,那么怎么做呢?

在包含两个滚动视图的视图控制器中实现UIScrollViewDelegate协议。 在里面:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 

委托方法,获取内容抵消:

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

然后设置另一个控件:

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

您可以通过在上面的委托方法中进行比较来确定要更改哪一个:

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

不必阅读

一般来说(至less从我所知道的情况来看)有两个UIScrollView / UITableVIew是不好的“风格”,因为它导致UI难以交互。 但是我认为如果你有足够有效的理由去做,那么我会告诉你一个办法。

码!

如果是我,那么我只是重写UIScrollViewtouchesMoved方法,并滚动另一个UIScrollView

在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) } 

希望这可以帮助

了解这个代码; 可能会帮助你。


 (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; } 
  • (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animation

如果(scrollView == scrollViewA)//改变B的偏移量else //改变A的偏移量