Making a scroll view scroll by itself?

Hi,


I'm trying to build a scrolling view that works sort of like a slot machine. When the user presses a button, I want the view to start scrolling and scroll for a while, then slow down and land on a result.


Is it possible to do this with a UIScrollView or its derivatives in iOS, or do I need to create the logic from scratch?


A UITableView does most of what I want, except that I need the scrolling action to start by itself, without the user having to swipe on the view.


Thanks,

Frank

You can set the contentOffset property in code; if you did that every frame (use CADisplayLink) I imagine you could get some smooth scrolling. Never tried it myself.


But unless you also need the goodies UIScrollView supplies (scroll indicators, response to user gestures with bouncing, deceleration etc.) then you might be better off just using plain UIViews.

>When the user presses a button, I want the view to start scrolling and scroll for a while, then slow down and land on a result.


Try:

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

...then use the x and y as the button's touch points on the screen you can capture.

You can also do an animation with CoreAnimation:


[UIScrollView beginAnimations:@"scrollAnimation" context:nil];
[UIScrollView setAnimationDuration:1.0f];
[scroll setContentOffset:CGPointMake(x, y)];
[UIScrollView commitAnimations];

Duration controls speed.

◅▻

If you are using a UITableView, consider using UITableView's following methods:


- scrollToRowAtIndexPath:atScrollPosition:animated:

- scrollToNearestSelectedRowAtScrollPosition:animated:


Sounds like the first one could work for you.

KMT,

will the above animation of contentOffset property cause the scrollbar indicator to be visible? If not, how could one make it visible?

Making a scroll view scroll by itself?
 
 
Q