Detecting UIScrollView Position
Asked Answered
P

5

9

I am trying to use a UIScrollView as a navigation device in my iOS application. Basically, I want the application to load specific content, based on the position of a paging-enabled UIScrollView. It is sort of like a navigation wheel.

Currently, I have a two-page (2*320) scrollview, and I am using the scrollViewDidEndDragging delegate method and contentoffset.x to load the appropriate content. I try to determine the position of the scrollview as such:

if (scrollView.contentOffset.x < 320) {
   // Load content 1
    }
else if (scrollView.contentOffset.x >= 320) {
   // Load content 2
    }

My problem is that I either do not understand how to work with contentoffset or it is not suitable to deal with my problem. Either way, I am stuck. Can someone let me know where I went wrong and/or show me a more efficient way to determine scrollview position?

I am stupid, I forgot to mention what the problem is... Basically, I cannot track the position of the scrollview. When I move to the second page it only registers changes if I go over the 620 limit (bounce rightward). But when I go back to the starting position (page 1), it does register the changes. In other words, my tracking is not accurate.

Delegate and everything else is working fine, I log them!

Peeress answered 23/12, 2012 at 16:7 Comment(0)
D
17

It's hard to say because you never specifically mentioned what the problem is, but I'll take a couple guesses at it.

If the delegate method isn't being called at all you need to remember to set the scroll views delegate:

[myScrollView setDelegate:self];

Otherwise, the problem with using scrollViewDidEndDragging to detect the scroll views offset is that it will check the offset at the point where you stop dragging which could be within the destination pages rect, or within the starting pages rect. As an alternative, I'd suggest you use scrollViewDidEndDecelerating to check the content offset as it will be called as soon as the scroll view comes to a stop at its destination page.

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x < 320) {
        NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
    }
    else if (scrollView.contentOffset.x >= 320) {
        NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
    }
}
Dasher answered 23/12, 2012 at 17:8 Comment(0)
Y
8

I think this could help you :). With scrollViewDidScroll you always get the exact position of your scrollView:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x < 320)
    {
         // Load content 1
    }
    else if (scrollView.contentOffset.x >= 320)
    {
         // Load content 2
    }
}

NSLog(@"Position: %g", scrollView.contentOffset.x);

Do not forget the delegate UIScrollViewDelegate in your .h file.

Yerxa answered 23/12, 2012 at 17:10 Comment(1)
Unfortunately is does not solve my problem. I added some extra info to clarify what the issue is.Peeress
D
4

Here a solution for getting the current position (page) as integer Value: Use scrollViewDidEndDecelerating: methode:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"%i", (int)(_scrollView.contentOffset.x / _scrollView.frame.size.width));
}
Daimon answered 22/9, 2014 at 22:17 Comment(0)
I
1

If for any reason, you'd like to be notified before the view ended decelerating; for example, to perform animations. You can also use this:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    // Where the scroll will end (vertically)
    let offSetY = targetContentOffset.pointee.y

    // I've got a scroll view with paging enabled so I use this to check which "page" is selected.
    if offSetY == firstPageView.frame.origin.y {
        // Do something…
    } else if offSetY == secondPageView.frame.origin.y {
        // Do something…
    }
}
Intermarriage answered 25/4, 2020 at 8:4 Comment(0)
F
0

Using UIScrollViewDelegate,

 func scrollViewWillBeginDragging(scrollView: UIScrollView){
        print("dragging begins")
        print("Position:  \(scrollView.contentOffset.x) , \(scrollView.contentOffset.y) ")
    }
Fauman answered 30/5, 2016 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.