3

I am creating an app where I am trying to have a pagescrollviewcontroller swipe through screens and have the top "nav" bar titles scroll real time with the titles i have the title bar view as a custom view and I am able to access the scroll delegate methods for both the custom scroll view and the pageview controller. However. I don't see how to access the real time scroll pos? I know it is possible because twitter does it (really can't shouldn't be in anyone's vocabulary) but I am not sure how to achieve this.

a pictureenter image description here

the home title swipes at the same scroll pos as the pagecontrollers.

current code:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if(scrollView.tag == 100) {
      CGPoint point = CGPointMake(mainScrollView.contentOffset.x * 1,0);
      [titleSwipe setContentOffset:point animated:YES];
    }
    else {
        mainScrollView.contentOffset = CGPointMake(0,1);
        [mainScrollView setContentOffset:titleSwipe.contentOffset animated:YES];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:     (BOOL)decelerate {

    CGPoint point = CGPointMake(mainScrollView.contentOffset.x * 2,0);

    [titleSwipe setContentOffset:point animated:YES];

}
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Dnaso
  • 1,335
  • 4
  • 22
  • 48
  • How are you accessing the UIPageViewController's scroll view delegate? Something like this: http://stackoverflow.com/a/19066412/705602 ? – mattsson Feb 21 '14 at 14:41
  • @mattson no in interface UIScrollView *v........ – Dnaso Feb 22 '14 at 12:31
  • @mattson then in viewdid load v = self.view.subviews[0]; the firs subview of apageveiw conttroller is a scrollvew so just synthesize a variable for it. one up my question if it helps! – Dnaso Feb 22 '14 at 12:32
  • That's a very risky way to do it, since your app would break if Apple decides to change the UIPageViewController view hierarchy. You should at least loop through the hierarchy to find it. – mattsson Feb 24 '14 at 10:48
  • yes i agree.. i stopped using pageviewcontroller anyway because you have no control over your sub controllers so I implemented my own. but then just loop through the subview of the pageview controller and if it is a type of scrollview then break the loop and that will be the scrollview. – Dnaso Feb 24 '14 at 15:32

1 Answers1

5

You'll find the real time position in scrollViewDidScroll:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // X scroll
    //
    CGFloat percentage = scrollView.contentOffset.x / scrollView.contentSize.width;
    NSLog(@"Scrolled percentage: %f", percentage);
}

Hope that helps!

jbouaziz
  • 1,484
  • 1
  • 12
  • 24
  • what a silly delegation method name . thank you 1000 times over. haha – Dnaso Feb 05 '14 at 23:21
  • thanks.. @pacman, just one more question don't mean to bother you but do you know why the pageviewcontroller has a weird content offset? it scrolls fine but if I am moving forward the content offset jumps forward 300 and visa versa going backwards, have you ever encountered that? – Dnaso Feb 05 '14 at 23:37
  • Hmm.. Not sure of what you mean but the pageViewController removes view when they're not visible anymore to optimize memory. That would explain why it suddenly jumps.I had to implement the same behavior as you did but for design implementation reasons, I reimplemented my own pageViewController. You can do it by creating a UIScrollView and adjust it's content size according to the number of controllers you have. Then by delegating this main scrollView, you get access to the global x offset. – jbouaziz Feb 06 '14 at 00:20
  • 1
    yeah I figured that, when it moves forward it adds not only the next controller but the controller AFTER and which I was getting an extra 320 px. and why when i went back it would jump -320. I guess I am going to have to do some math and tinkering =( boo hoo – Dnaso Feb 06 '14 at 00:36