ios - When is the right time to animate and add sublayers in an UIView subclass -
if create subclassed uiview
, want use bunch of custom made content (animations via cabasicanimation
using cashapelayer
s, custom made drawings using cgcontextref
in drawrect
) when right time create, add , animate sublayers?
i know should perform custom drawings in drawrect
method (and since there place when can uigraphicsgetcurrentcontext()
kinda narrows choice down). have been creating sublayers, animations , other non-drawing related stuff in drawrect
well. i'm not sure drawrect
best place kind of activities.
i familiar layoutsubviews
method , other uiview
methods haven't implemented of them except drawrect
.
so if repeat myself 1 more time - question goes: add sublayers, animate them , there tricks or catches should aware of?
you can create , add layers @ init, if permanent. if dynamic, layoutsubviews better. means need setneedslayout whenever new layer/item required. can mix , match as want between -init , -layoutsubviews, if had pick, i'd lean toward using layoutsubviews. don't use drawrect.
you can set properties (strokewidth, linecolor, path, etc) cashapelayer @ either time of creation or during normal execution. includes setting path cashapelayer. again, don't set properties in drawrect.
if want custom drawing on layer can subclass layer , use drawrect on layer. can if calayers need reused , extended. can supply calayerdelegate, long not uiview. see these questions: using calayer delegate and ios: using uiview's 'drawrect:' vs. layer's delagate 'drawlayer:incontext:'
animation easy, , follows same principles creating , setting properties. make sure understand when automatic animations invoked , how disable them: disabling implicit animations in -[calayer setneedsdisplayinrect:] again don't try animate drawrect: how make uibezierpath animated cashapelayer?
drawing expensive. animation cheap. using drawrect cause big performance hit applications. use drawrect/setneedsdisplay sparingly, instance on state change (selected/unselected). whenever possible modify views animations or top level properties, , don't redraw view. making drawrect other draw result in calling setneedsdisplay unnecessarily. easy upfront optimization call drawrect little possible, or not @ all.
Comments
Post a Comment