ios - How to prevent an animation from completing when user leaves and returns to app -


i making app have button moving 1 side of screen other. have created pause button pauses animation once button selected. button , function works fine. added function determines if user has exited application. in function, added pause button function. however, when user returns app after exiting, shows pause screen, animation completed. here code:

@ibaction func pausebutton(sender: uibutton) {     let button = self.view.viewwithtag(1) as? uibutton!     let layer = button!.layer     pauselayer(layer) // pausing animation     view2.hidden = false // showing pause screen } func pauselayer(layer: calayer) {     let pausedtime: cftimeinterval = layer.converttime(cacurrentmediatime(), fromlayer: nil)     layer.speed = 0.0     layer.timeoffset = pausedtime } @ibaction func startbutton(sender: uibutton) {     let button = uibutton()     button.tag = 1 // can retrieve in pause function     // code make button nice     button.translatesautoresizingmaskintoconstraints = false // allow constraints     let topanchorforbutton = button.topanchor.constraintequaltoanchor(view.topanchor, constant: 400)     nslayoutconstraint.activateconstraints([ // adding constraints         topanchorforbutton,         button.leadinganchor.constraintequaltoanchor(view.topanchor, constant: 120),         button.widthanchor.constraintequaltoconstant(65),         button.heightanchor.constraintequaltoconstant(65)         ])     self.view.layoutifneeded()     [uiview.animatewithduration(5.0, delay: 0.0, options: [.curvelinear, .allowuserinteraction], animations: {         topanchorforbutton.constant = 0         self.view.layoutifneeded()         }, completion: nil )] // animation  override func viewdidload() {     super.viewdidload()          nsnotificationcenter.defaultcenter().addobserver(self, selector: "pausebutton:", name: uiapplicationwillresignactivenotification, object: nil) // when exists app          nsnotificationcenter.defaultcenter().addobserver(self, selector: "pausebutton:", name: uiapplicationdidenterbackgroundnotification, object: nil) // when goes home screen } 

when re-enter app, animation completed.

here's code i've tried plays , stops animation. i've added in app delegate still doesn't work:

viewcontroller:

class home: uiviewcontroller {  func pauseanimation(){ let button = self.view.viewwithtag(1) as? uibutton! let layer = button!.layer pauselayer(layer) // pausing animation }   @ibaction func pausebutton(sender: uibutton) {    pauseanimation() } func pauselayer(layer: calayer) {     let pausedtime: cftimeinterval = layer.converttime(cacurrentmediatime(), fromlayer: nil)     layer.speed = 0.0     layer.timeoffset = pausedtime } @ibaction func startbutton(sender: uibutton) {     let button = uibutton()     button.tag = 1 // can retrieve in pause function     // code make button nice     button.settitle("moving", forstate: .normal)     button.settitlecolor(uicolor.redcolor(), forstate: .normal)     view.addsubview(button)     button.translatesautoresizingmaskintoconstraints = false // allow constraints     let topanchorforbutton = button.topanchor.constraintequaltoanchor(view.topanchor, constant: 400)     nslayoutconstraint.activateconstraints([ // adding constraints         topanchorforbutton,         button.leadinganchor.constraintequaltoanchor(view.topanchor, constant: 120),         button.widthanchor.constraintequaltoconstant(65),         button.heightanchor.constraintequaltoconstant(65)         ])     self.view.layoutifneeded()     [uiview.animatewithduration(10.0, delay: 0.0, options: [.curvelinear, .allowuserinteraction], animations: {         topanchorforbutton.constant = 0         self.view.layoutifneeded()         }, completion: nil )] // animation } 

app delegate:

class appdelegate: uiresponder, uiapplicationdelegate {  var window: uiwindow? var vc = home() func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { } func applicationwillresignactive(application: uiapplication) {    vc.pauseanimation() } 

just in case want know, here code resuming animation:

@ibaction func resume(sender: uibutton) {     let button = self.view.viewwithtag(100) as? uibutton!     let layer = button!.layer      resumelayer(layer) } func resumelayer(layer: calayer) {     let pausedtime: cftimeinterval = layer.timeoffset     layer.speed = 1.0     layer.timeoffset = 0.0     layer.begintime = 0.0     let timesincepause: cftimeinterval = layer.converttime(cacurrentmediatime(), fromlayer: nil) - pausedtime     layer.begintime = timesincepause } 

when exit app, says button or/and layer nil (fatal error... optional value nil), crashes.

please help. in advance... anton

you need know little how animations work under hood. explained in wwdc session 236 'building interruptible , responsive interactions' (the part interested in starts @ 17:45). in meantime, read cabasicanimation, , checkout presentationlayer property on calayer (you need drop down calayer want achieve, , maybe need implement animations using ca(basic)animation, although maybe uikit's animation methods might still able trick, if you've structured code well).

basically, view or layer end-values set part of animation immediately. animation starts, begin end. if animation gets interrupted, leaving app, upon return view gets drawn values has (which end-values, animation gone).

what want capture value of view @ point in animation when leave app, save it, , start new animation point original endpoint when app comes foreground. can current position of calayer (not uiview) asking frame, or transform, whatever need, calayer's presentationlayer property.

i had problem when building custom refreshcontrol use in `uiscrollview``. code on github, relevant code here (its objective-c, not complicated, should able follow along , re-implement need in swift): jrtrefreshcontrolview.m, specifically:

-(void)willmovetowindow:(uiwindow *)newwindow gets called when user moves app background, or when view removed superview (newwindow nil).

and actual implementation of animation in subclass here: jrtrotatingrefreshcontrolview.m,

- (void) resumerefreshingstate method want at.

(there saverefreshingstate, can save presentation-values presentationlayer, not implemented animations).


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -