Understanding Layout method custom views
I just wondered if someone could shed any light on the workings of the layout method in custom views for me. I get some inconsistent results. I am not sure if the inner workings of the code that's calling the layout method is doing some sort of smart caching of multiple events or not. If so it would explain why I don't get a one to one correlation to the views I resize to the number of times layout() is called. This might be a stupid question, especially if calls to layout are being cached. But is there anyway directly or indirectly to know in the layout() method what view had caused the event to be called. I realise, I could hide the subviews in the custom class and provide methods to manipulate them. Not really ideal though. I guess the other method would be to redraw all the subviews based on the new sizes.
import ui class test(ui.View): def __init__(self): self.background_color= 'white' self.sv = ui.View(frame=(0,0,100,100)) self.sv.background_color ='red' self.add_subview(self.sv) self.sv2 = ui.View(frame=(0,100,100,100)) self.sv2.background_color ='yellow' self.add_subview(self.sv2) def layout(self): print 'layout fired' def will_close(self): print 'closing view' if __name__ == '__main__': v = test() v.sv.background_color = 'green' print 'about to change sv2 height' v.sv2.height = v.height / 2 print 'about to change sv width' v.sv.width = v.width /2 v.present('sheet') print 'about to change sv width ' v.sv2.width= 300 v.sv2.height = 100
Update: From what I can see, layout is dangerous when using ui.animate. I am not sure, but it looks as if it would be hard to code it cleanly. Although only getting one layout update when resizing a subview to zero width, you get the layout call immediately after the animation starts(I understand it's threaded) It seems like there should be a method you can poll if the current or all animations are finished. I guess you Could use state variables and check the dimension you are animating, but the code will get a lot more specific and messy, unless I am missing something here
To help support the assumption made the post before with ui.animate
import ui import time class test_ani(ui.View): def __init__(self): self.width = 540 self.height = 576 self.sv1 = None self.sv2 = None self.background_color = 'white' btn = ui.ButtonItem('Animate') btn.action = self.slide_animation_start self.right_button_items = [btn,] self.sv1 = ui.View() self.sv1.frame = self.frame self.sv1.width = self.width / 2 self.sv1.background_color = 'maroon' self.add_subview(self.sv1) self.sv2 = ui.View() self.sv2.frame = self.sv1.frame self.sv2.width = self.width / 2 self.sv2.x = self.sv1.width self.sv2.background_color = 'lime' self.add_subview(self.sv2) def slide_animation_start(self, sender): print 'animation started at ', time.ctime() # do very slowly, to illustrate the issue ui.animate(self.my_slide_animation, duration = 5) #layout is called around here i guess def my_slide_animation(self): self.sv1.width = 0.0 self.sv2.width = self.width self.sv2.x = self.sv1.width def layout(self): print 'Layout fired' def will_close(self): print time.ctime() if __name__ == '__main__': v = test_ani() v.present('sheet')
Hmmm, ok a fair comment would be then write your own animation routine. But ui.animate() is nice and free :) threading, duration and acceleration/de-acceleration all done for you. Nice