xcode 两个滚动视图同时工作,一键式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8306753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Two scroll view work Simultaneous with one touch
提问by sinh99
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?
如果这是可能的,那么怎么做呢?
回答by TigerCoding
Implement the UIScrollViewDelegate protocol in the view controller containing both scroll views. In the:
在包含两个滚动视图的视图控制器中实现 UIScrollViewDelegate 协议。在里面:
- (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
回答by Jab
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.
通常(至少从我所知道的情况来看),将 2 UIScrollView
/UITableVIew
放在一起是不好的“风格”,因为它会导致 UI 难以交互。但我认为,如果您有足够有效的用途/理由来做这件事,那么我会向您展示一种完成它的方法。
CODE!
代码!
If it was me then I'd just override the UIScrollView
's touchesMoved
method and scroll the other UIScrollView
that way.
如果是我,那么我只需覆盖UIScrollView
'stouchesMoved
方法并以UIScrollView
这种方式滚动另一个。
Within scrollView_1
在 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
希望这可以帮助
回答by Ketan Patel
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
- (void)setContentOffset:(CGPoint)contentOffset 动画:(BOOL)动画
if( scrollView == scrollViewA ) // change offset of B else // change offset of A
if( scrollView == scrollViewA ) // 改变 B 的偏移量 else // 改变 A 的偏移量
回答by Ketan Patel
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;
}