Objective-C: manually swap UIScrollView
I want to use scrollview as something like a collector in horizontal mode. Scrollview has up to seven sub-zones. Each subview represents a value. Three views are always visible, and one of them is the selected one.
Scroll view at start:
__ | V1 | V2
Scrollview is set to view / value two:
V1 | V2 | V3
Scrollview is set to its last value:
V2 | V3 | __
The real problem I am facing is the "pagingEnabled" flag. If pagingEnabled is set to YES, then scroll pages always have three subsets / values instead of one. If pagingEnabled is set to NO, the scrollview is not pinched.
Is there a good solution for my problem?
Thanks a lot Dan;)
a source to share
I found a solution in case anyone else is interested.
Assign you a scroll delegate view. Ovveride scrollViewDidEndDecelerating, after that get the current index (the page you want) by doing something like.
NSNumber* currentIndex = [NSNumber numberWithInt:round(scrollview.Contentoffset.x / PAGE_SIZE)];
//Then just update your scrollviews offset with
[scrollview setContentOffset:CGPointMake([currentIndex intValue] * PAGE_SIZE, 0) animated:YES];
a source to share
As of iOS 5, there is scrollViewWillEndDragging:withVelocity:targetContentOffset:
a delegation method for UIScrollViewDelegate
. This allows for arbitrary paging.
To do this, you first need to set the property pagingEnabled
to a value NO
, otherwise the delegate method I am talking about is not called. Scrolling now calls this delegate method whenever the user lifts their finger and the scroll view wants to determine where to end the scrolling.
Magic is the last argument,: targetContentOffset
it is a pointer to CGPoint
and is used as an in / out variable. This means that this variable tells you where the scroll is required. But it allows you to change this target location. velocity
may also be of interest, it can give you an indication, "the user" pressed "scroll" or moved it, stopped, and then lifted his finger.
For example, here's an implementation that rounds the target location x
to the nearest multiple of 100, thus making "pages" 100 points wide.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
targetContentOffset->x = round(targetContentOffset->x / 100.0) * 100.0;
}
a source to share