ios - UIViewControllerAnimatedTransitioning: screen keeps getting black under custom view -
i have found many related topics on (and elsewhere) still couldn't find solution problem. want display custom alert on view using uiviewcontrollertransitioningdelegate
. first, in initial view controller, here call:
@ibaction func tappedbutton(_ sender: any) { myalertviewcontroller.presentin(viewcontroller: self) }
and here code of myalertviewcontroller
:
import uikit open class myalertviewcontroller: uiviewcontroller, uiviewcontrollertransitioningdelegate { @iboutlet weak var overlayview: uiview? @iboutlet weak var alertview: uiview? @iboutlet weak var alertcenteryconstraint: nslayoutconstraint? public override init(nibname nibnameornil: string?, bundle nibbundleornil: bundle?) { super.init(nibname: "myalertview", bundle: nil) self.transitioningdelegate = self } required public init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } static func presentin(viewcontroller: uiviewcontroller) { let alertviewcontroller = myalertviewcontroller() if thread.ismainthread { viewcontroller.present(alertviewcontroller, animated: true, completion: nil) } else { dispatchqueue.main.async { viewcontroller.present(alertviewcontroller, animated: true, completion: nil) } } } public func animationcontroller(fordismissed dismissed: uiviewcontroller) -> uiviewcontrolleranimatedtransitioning? { return mydismissalertviewanimationcontroller() } public func animationcontroller(forpresented presented: uiviewcontroller, presenting: uiviewcontroller, source: uiviewcontroller) -> uiviewcontrolleranimatedtransitioning? { return mypresentalertviewanimationcontroller() } } class mypresentalertviewanimationcontroller: nsobject, uiviewcontrolleranimatedtransitioning { public func transitionduration(using transitioncontext: uiviewcontrollercontexttransitioning?) -> timeinterval { return 0.3 } public func animatetransition(using transitioncontext: uiviewcontrollercontexttransitioning) { let toviewcontroller: myalertviewcontroller = transitioncontext.viewcontroller(forkey: uitransitioncontextviewcontrollerkey.to) as! myalertviewcontroller let duration = self.transitionduration(using: transitioncontext) let containerview = transitioncontext.containerview toviewcontroller.view.frame = containerview.frame containerview.addsubview(toviewcontroller.view) toviewcontroller.overlayview?.alpha = 0.0 uiview.animate(withduration: duration, animations: { toviewcontroller.overlayview?.alpha = 0.6 }) let finishframe = toviewcontroller.alertview?.frame var startingframe = finishframe startingframe?.origin.y = -((finishframe?.height)!) toviewcontroller.alertview?.frame = startingframe! uiview.animate(withduration: 0.5, delay: 0.0, usingspringwithdamping: 0.7, initialspringvelocity: 1.0, options: .layoutsubviews, animations: { toviewcontroller.alertview?.frame = finishframe! }, completion: { result in transitioncontext.completetransition(result) }) } } class mydismissalertviewanimationcontroller: nsobject, uiviewcontrolleranimatedtransitioning { public func transitionduration(using transitioncontext: uiviewcontrollercontexttransitioning?) -> timeinterval { return 0.3 } public func animatetransition(using transitioncontext: uiviewcontrollercontexttransitioning) { let fromviewcontroller: myalertviewcontroller = transitioncontext.viewcontroller(forkey: uitransitioncontextviewcontrollerkey.from) as! myalertviewcontroller let duration = self.transitionduration(using: transitioncontext) uiview.animate(withduration: duration, animations: { fromviewcontroller.overlayview?.alpha = 0.0 }) var finishframe = fromviewcontroller.alertview?.frame finishframe?.origin.y = -(finishframe?.height)! finishframe?.origin.y = fromviewcontroller.isdismissingbybottom ? fromviewcontroller.view.frame.size.height : -(finishframe?.height)! uiview.animate(withduration: duration, delay: 0.0, usingspringwithdamping: 1.0, initialspringvelocity: 1.0, options: .layoutsubviews, animations: { fromviewcontroller.alertview?.frame = finishframe! }, completion: { result in transitioncontext.completetransition(true) }) } }
the animation works fine, black screen appears after call completetransition()
can see below:
thanks help...
i think need either set background color so:
self.view.backgroundcolor = .clear
this make sure background see not background color of modal.
or prevent presenting view controller removed screen making modal presentation style overcurrentcontext
self.modalpresentationstyle = .overcurrentcontext
Comments
Post a Comment