ios - Forcing a UIView to redraw its contents inside of an animation block -
i have superview , subview b. in a's drawrect(_:)
line drawing depends on position , size of subview b. in class (but not in drawrect(_:)
) have animation block moves , scales b.
the problem drawing in drawrect(_:)
happens before animation takes place, either using final position , size of subview or using initial position , size, never using intermediate values.
case 1: drawing uses final state
the image below screen shot of simulator while animation scales subview in progress. note how lines drawn in final state.
the code line drawing in a's drawrect(_:)
. code animation (it's in class self
refers superview):
uiview.animatewithduration(3.0, delay: 0.5, options: uiviewanimationoptions.curvelinear, animations: { [unowned self] in self.subviewwidthconstraint.constant = 0.75 * cgrectgetwidth(self.bounds) self.layoutifneeded() self.setneedsdisplay() // has no effect since self's bounds don't change }) { (completed) -> void in }
case 2: drawing uses initial state
looking around answers found suggestion use self.layer.displayifneeded()
in place of self.setneedsdisplay()
in animation block, tried well
func animateframe() { uiview.animatewithduration(3.0, delay: 0.5, options: uiviewanimationoptions.curvelinear, animations: { [unowned self] in self.subviewwidthconstraint.constant = 0.75 * cgrectgetwidth(self.bounds) self.layoutifneeded() self.layer.displayifneeded() // difference here }) { (completed) -> void in } }
but results in following. again, screen shot while animation scales subview in progress. now, lines again drawn before animation starts using subview's initial position , size.
i think understand case 1. entire animation block queued execution end result computed time animation starts it's no surprise drawinrect(_:)
using final state.
i don't understand case 2 that's because have little experience dealing directly view layer.
either way, effect i'm looking achieve lines drawn vertices of subview while it's being moved and/or scaled. suggestions or pointers documentation appreciated. thanks!
Comments
Post a Comment