3
votes

I have a UIScrollView with no vertical scrolling, and on which I force a particular offset (only x) in case the offset tries to become less that that particular offset. I use setContentOffset:animated: function, with animated argument as YES. The offset is forced correctly.

After I force the offset, 'sometimes' scrolling in the direction opposite to the initial scroll direction gets blocked. Say, I was scrolling with finger pan from left to right, and forced it to some offset, then I can't scroll from right to left anymore.

But the catch is, if I make any tap on the screen, the scrolling starts happening. I am unable to pan, but if I tap the screen, or tap any button, scrolling starts working. If I try to call the button press method programatically after, say 5 seconds of forcing the offset, then it doesn't work. It seems that I need to touch the screen somehow..

I checked the values of contentOffset, contentSize, they seem fine.

PS: there are times when scrollViewDidEndScrollingAnimation: method is not called after forcing the offset, but that is not necessarily the issue.

Edit: Actually, this thing happens when I take the scroll view beyond the threshold offset using my fingers, and keep panning left. At the threshold point, panning stops, but the next time I try to scroll, it doesn't pan. (i.e., I don't give a jerk to go beyond the threshold).

Edit: One more possible loophole: I make scrollEnabled equal to NO just before forcing the new offfset, and just after giving the command to set the new offset, I set it back to YES. I needs to be done so that if I try to scroll the scrollview with a jerk, it doesn't scroll away to the left while trying to set the new offset, since paging is enabled.

Edit: Could it be because I set scrollEnabled to NO while actually scrolling using touch? I do enable it later, but maybe that is some issue...

Important Edit: If I long press on the scrollView, and then try to move, scroll view starts scrolling!

Edit: This code is in scrollViewDidScroll:

if ((theScrollView.contentOffset.x < theScrollView.frame.size.width)
        && [currentlyDisplayedVC isEqual:VC1])
    {
        //if this is not done, and this call happens when VC3
        //is visible a bit too much, scrollView scrolls till VC3.
        scrollView.scrollEnabled = NO;

        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:YES];
        scrollView.scrollEnabled = YES;

        //this is done so that this block is not reached everytime during scroll animation.
        currentlyDisplayedVC = VC2;
    }

Then in scrollViewDidEndWithANimation: I add [self VC2reached]

3
Wait, you're saying that if you pan in a direction such that the you force the contentOffset programmatically, then you can't keep panning? What if you lift your finger up and then start panning again? Does that work?micantox
No, that doesn't. I lift my finger and try to pan, rotate the device and try to pan, nothing works.neeraj
Actually, this thing happens when I take the scroll view beyond the threshold offset using my fingers, and keep panning left. At the threshold point, panning stops, but the next time I try to scroll, it doesn't pan. (i.e., I don't give a jerk to go beyond the threshold).neeraj
If you post some code we could probably resolve this for you quite easily.Gallonallen
I have added the relevant piece of code...neeraj

3 Answers

1
votes

Have you tried using setUserInteractionEnabled:NO and YES to stop/allowing user to interact with the UIScrollView instead of using the scrollEnabled property?

1
votes

I subclassed UIScrollView and overrode the method touchesShouldBegin:withEvent:inContentView:. In the method I return NO for that particular moment when I see the view stuck, so that all touches are taken in by the scroll view instead of passing them to any other contentView. That solved the problem.

Edit: That doesn't solve the problem actually. I want to disallow touches on contentView ONLY when an attempt to scroll the scrollview is being made, not when some other event like tap on the contentView is attempted. But in ths olution that I posted, all touches will be kept with the scroll view. Do we have a similar touchMoved: method?

1
votes

Instead of using setContentOffset:animated:

you should try using scrollRectToVisible:animated: