Help with using ui.View methods?
Brian last edited by
What I am trying to do is create a ui with a single button. Upon the first button press a timer starts, which after 5 seconds closes the view. I want to measure the number of times the user can tap the button in 5 seconds. I display my ui (which I made in the visual ui editor) using the ui.load_view()
But where I am fuzzy is when I have to close the view, and how to use the timer objects.
I'm also programming in an object oriented style if that matters. I need the button I am trying to create to be reusable. Can anyone shed light on how I could tackle this problem?
JonB last edited by
You have two obvious options for timers:
ui.delay. Both work similar, you provide a function (even a locally defined function, lambda, etc) that gets executed at the time you specify. Delay is slightly easier to use.
You essentially want an initial action on first button press (start timer and increment counter) and a different one for the subsequent presses. One approach would be to simply have an if statement in your button action, which checks if the counter is zero, in which case it starts the timer. Another approach would be modify the
actionof your button to point to a new function.
The function that you pass to
ui.delaywould just call close() on your view, and might print out the counter, and might reset everything so that you could present the view again.
Note that you cannot actually subclass ui.button. You could create a custom view class which, upon init, simply creates a button that fills it's frame, and sets flex to WH so you can adjust the button size by changing your custom class size.
Brian last edited by
Thanks for the response!
How would I go about calling view? Do I need to set up an instance of the ui.View class, and then call it using that? I've been fiddling with it for a while now and cannot seem to get the view to close. The official documentation is a bit vague for a beginner like myself, so it's a possibility something from there went over my head.
Here's what I have:
v=ui.View() def button_tapped(sender): if b.taps<1: ui.delay(b.cw,1) b.taps+=1 class battleui: taps=0 ui.load_view("Brian's Adventures ab").present('sheet') def cw(self): v.close() b.taps=0
brumm last edited by