Here's a tricky iPhone problem I've been working on. I have three UIScrollViews on a page, one that only scrolls horizontally, one that only s开发者_JAVA百科crolls vertically, and one that scrolls both horizontally and vertically. I want to lock the views together, so that the horizontal location of the horizontal only scrollview matches the horizontal location of the main scrollview, and the vertical scrollview likewise, so that dragging the main scrollview around controls the horizontal and vertical scrollviews.
Problem is, I'm completely stumped. I've looked around inside the apple documentation, but there doesn't seem to be any way to do this officially. One thought that I had was to somehow "clone" any touch on any of the scrollviews to a point on the other two, but I have no idea how to do this. If anyone has any thoughts on this, I'd very much appreciate it.
Edit: I tried the suggestion of subclassing UIScrollView and overriding touchesMoved to call touchesMoved on the other scroll views. Unfortunately, touchesMoved doesn't get called for scrolling motions as UIScrollView intercepts those motions somehow and uses them to control its scrolling. I started looking around more low level stuff and found the core animation scroll layers, but ideally I wouldn't want to recreate UIScrollView from scratch. Still trying to figure this one out.
I think the simplest way would be to add a delegate to your scrollviews which implement the following method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
Then you can check the contentOffset to check by how much the scrollview did actually scroll, and update the main scrollview accordingly.
You need to extend UIScrollView for all three uses on your screen and override the touchesMoved:withEvent: methods to call the other ones (make sure you don't end up with circular code that ends up calling itself again!).
So, for example the code in the two-way scrolling item could be based on this:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[horizontalScrollview touchesMoved:touches withEvent:event];
[verticalScrollview touchesMoved:touches withEvent:event];
}
by using some additional variable (eg ControllerLastTouched
) you can check that before calling the other two touchesMoved:withEvent: methods.
This should enable you to do the "cloning" you were talking about...
精彩评论