ios - complateTransition don't dismiss the from view swift 2 -
i trying set interactive transition class :
class transitionmanager: nsobject, uiviewcontrolleranimatedtransitioning, uiviewcontrollerinteractivetransitioning, uiviewcontrollertransitioningdelegate, uiviewcontrollercontexttransitioning { weak var transitioncontext: uiviewcontrollercontexttransitioning? var sourceviewcontroller: uiviewcontroller! { didset { enterpangesture = uiscreenedgepangesturerecognizer() enterpangesture.addtarget(self, action:"panned:") enterpangesture.edges = uirectedge.left sourceviewcontroller.view.addgesturerecognizer(enterpangesture) } } var togosourceviewcontroller: uiviewcontroller! let duration = 1.0 var presenting = true var reverse = false var originframe = cgrectnull var shouldbeinteractive = false private var didstartedtransition = false private var animated = false private var interactive = false private var animationstyle = uimodalpresentationstyle(rawvalue: 1) private var didfinishedtransition = false private var percenttransition: cgfloat = 0.0 private var enterpangesture: uiscreenedgepangesturerecognizer! private var tovc = uiviewcontroller() private var pointtovc = cgpoint() private var pointfromvc = cgpoint() private var fromvc = uiviewcontroller() private var generalcontainer = uiview() func animatetransition(transitioncontext: uiviewcontrollercontexttransitioning) { animated = true let container = transitioncontext.containerview() let fromviewcontroller = transitioncontext.viewcontrollerforkey(uitransitioncontextfromviewcontrollerkey)! let toviewcontroller = transitioncontext.viewcontrollerforkey(uitransitioncontexttoviewcontrollerkey)! if reverse { toviewcontroller.view.center.x -= (container?.bounds.size.width)! container?.insertsubview(toviewcontroller.view, abovesubview: fromviewcontroller.view) } else { toviewcontroller.view.center.x += (container?.bounds.size.width)! container?.addsubview(toviewcontroller.view) } uiview.animatewithduration(duration, delay: 0.0, options: uiviewanimationoptions.transitionnone, animations: { if self.reverse { toviewcontroller.view.center.x += (container?.bounds.size.width)! } else { toviewcontroller.view.center.x -= (container?.bounds.size.width)! } }, completion: { finished in transitioncontext.completetransition(true) self.animated = false self.reverse = !self.reverse }) } func transitionduration(transitioncontext: uiviewcontrollercontexttransitioning?) -> nstimeinterval { return duration } func startinteractivetransition(transitioncontext: uiviewcontrollercontexttransitioning) { interactive = true animated = true let container = transitioncontext.containerview() let fromviewcontroller = transitioncontext.viewcontrollerforkey(uitransitioncontextfromviewcontrollerkey)! //articleview let toviewcontroller = transitioncontext.viewcontrollerforkey(uitransitioncontexttoviewcontrollerkey)! //article ou favoris if reverse { toviewcontroller.view.frame.origin.x = -fromviewcontroller.view.frame.maxx container?.insertsubview(toviewcontroller.view, abovesubview: fromviewcontroller.view) } tovc = toviewcontroller pointtovc = toviewcontroller.view.bounds.origin fromvc = fromviewcontroller pointfromvc = fromviewcontroller.view.bounds.origin generalcontainer = container! } func containerview() -> uiview? { return sourceviewcontroller?.view } func viewcontrollerforkey(key: string) -> uiviewcontroller? { return sourceviewcontroller?.storyboard!.instantiateviewcontrollerwithidentifier(key) } func viewforkey(key: string) -> uiview? { return sourceviewcontroller?.storyboard!.instantiateviewcontrollerwithidentifier(key).view } func initialframeforviewcontroller(vc: uiviewcontroller) -> cgrect { return vc.view.frame } func finalframeforviewcontroller(vc: uiviewcontroller) -> cgrect { return vc.view.frame } func isanimated() -> bool { return animated } func isinteractive() -> bool { return interactive } func presentationstyle() -> uimodalpresentationstyle { return animationstyle! } func completetransition(didcomplete: bool) { interactive = false animated = false shouldbeinteractive = false didfinishedtransition = didcomplete transitioncontext?.finishinteractivetransition() transitioncontext?.completetransition(true) } func updateinteractivetransition(percentcomplete: cgfloat) { if self.reverse { print(percentcomplete) self.tovc.view.frame.origin.x = (self.fromvc.view.frame.maxx * (percentcomplete)) - self.fromvc.view.frame.maxx } } func finishinteractivetransition() { uiview.animatewithduration(duration, delay: 0.0, options: uiviewanimationoptions.transitionnone, animations: { if self.reverse { self.tovc.view.frame.origin.x = self.fromvc.view.frame.origin.x } }, completion: { finished in self.animated = false self.reverse = !self.reverse self.completetransition(true) }) } func cancelinteractivetransition() { uiview.animatewithduration(duration, delay: 0.0, options: uiviewanimationoptions.transitionnone, animations: { if self.reverse { self.tovc.view.frame.origin.x = -self.fromvc.view.frame.maxx } }, completion: { finished in self.animated = false self.completetransition(true) }) } func transitionwascancelled() -> bool { return didfinishedtransition } func targettransform() -> cgaffinetransform { return cgaffinetransform() } func completionspeed() -> cgfloat { return 1 - percenttransition } func panned(pan: uipangesturerecognizer) { switch pan.state { case .began: animated = true shouldbeinteractive = true didstartedtransition = true didfinishedtransition = false sourceviewcontroller?.dismissviewcontrolleranimated(true, completion: nil) updateinteractivetransition(0) break case .changed: percenttransition = cgfloat(pan.translationinview(sourceviewcontroller!.view).x / sourceviewcontroller!.view.frame.width) if percenttransition < 0.0 { percenttransition = 0.0 } else if percenttransition > 1.0 { percenttransition = 1.0 } updateinteractivetransition(percenttransition) break case .ended, .failed, .cancelled: animated = false shouldbeinteractive = false didstartedtransition = false didfinishedtransition = true if percenttransition < 0.8 { cancelinteractivetransition() } else { finishinteractivetransition() } break case .possible: break } }
}
the animatetransition
works , dismiss fromviewcontroller during interactivetransition
when call finishinteractivetransition()
, completetransition(true)
, still have both view :
but on apple said :
you must call method after animations have completed notify system transition animation done. parameter pass must indicate whether animations completed successfully. interactive animations, must call method in addition finishinteractivetransition or cancelinteractivetransition method. best place call method in completion block of animations.
so, doing wrong ?
i using ios9, swift 2, xcode 7 beta 6
i found solution :
i should call transitioncontext.completetransition(true)
in function finishinteractivetransition()
on previous code transitioncontext
not same in startinteractivetransition(transitioncontext: uiviewcontrollercontexttransitioning)
so add 1 variable :
private var context: uiviewcontrollercontexttransitioning?
and use call :
transitioncontext.completetransition(true)
or
transitioncontext.completetransition(false)
Comments
Post a Comment