Curnelious
Curnelious

Reputation: 1

UIScrollView that scrolls only programmatically

We need to make a scroll view that is scrolling only when user swipe at some area on screen, which is NOT inside the scroll view .

So i have created a scroll view that working great ,now i am struggling to find a way to disable the scrolling from within the scroller and somehow read the user swiping in that relevant area(some strip view), and than send that swipes to the scroller, than scroll programmatically with:

[scrollView setContentOffset:CGPointMake(x, y) animated:YES];

Is that the right way to achieve this , or there is some out of the box way ?

How would i disable the scrolling option inside the scroller ( without disabling touches inside him-because he has some tableView inside), and than just scroll programatically?

EDIT ::

I set the

[self.scrollView setPagingEnabled:YES];
[self.scrollView setScrollEnabled:NO];

than no scrolling enabled. than when swipe in that view i am trying to move the scroller with:

  **[self.scrollView scrollRectToVisible:CGRectMake(scrollXPo+10, y, width, height) animated:YES];**

But, its moving the 10 pixels and than its not moving anymore, also the paging is not working in this constellation ..

EDIT2

Changed it into :

self.scrollView.contentOffset.x+40

works, but no paging yet ..

How do i fix this little things ?

Upvotes: 1

Views: 850

Answers (2)

Lukas Kukacka
Lukas Kukacka

Reputation: 7704

You can set UIScrollView's scrollEnabled to NO to disable scrolling by touches, but still keep the possibility to scroll in code. Then you can just use setContentOffset:animated to scroll view to the relevant visible area according to you needs (swipine in that relevant area).

As UIScrollView's documentation says:

A Boolean value that determines whether scrolling is enabled. If the value of this property is YES , scrolling is enabled, and if it is NO, scrolling is disabled. The default is YES. When scrolling is disabled, the scroll view does not accept touch events; it forwards them up the responder chain.

Upvotes: 1

Usman Nisar
Usman Nisar

Reputation: 3081

You can get view info on touch event, so simply get touched view and check if touched view is scrollview/views with in it, then do not perform scrolling

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([[touch view] isKindOfClass:[UIScrollView class]]) {
        //do not perform programmatically scrolling
    }
    else
   {
       //do your scrolling here
   }
}

Upvotes: 0

Related Questions