Forum Archive

Animate content offset

donnieh

I am not having much luck animating content offset. When the view loads I want the scrollview to glide to the offset position. Any tips implementing the ui.animate() function.

​
import ui
w, h = ui.get_screen_size()
sv = ui.ScrollView()
sv.frame = (0,0,w,h)
sv.background_color = 'gray'
sv.content_size = (0, 2000)
y_offset = 1000
sv.present()
sv.content_offset = (0, y_offset) #animate me
omz

Something like this should work:

# ...
def scroll():
    sv.content_offset = (0, y_offset)
ui.animate(scroll, 0.5)
donnieh

Great thanks. Now I get it.

omz

Alternative, if you don't like nested functions:

from functools import partial
ui.animate(partial(setattr, sv, 'content_offset', (0, y_offset)), 0.5)

(I find this a bit harder to read though.)

donnieh

Oooo. Cleaver. That is definitely for the pros. I like it.

JonB

iirc lambdas also work, and might be slightly easier to read than partial.