2

I have an animation of SCNNodes triggered by a button. On a subsequent press of the button, the animation reverses. The forward animation always works fine But the reverse animation will sometimes hang up. That is, it will play the portion before the completion handler then freeze. But when I apply any of my three gestures (pan, translate, zoom) it un-hangs and completes the handler portion. The relevant code:

            else if buttonToggle_1 == true {  buttonToggle_1 = false
                let moveWaterBack = SCNAction.moveTo(homePosition_2, duration: 1)
                let moveValineBack = SCNAction.moveTo(awayPosition_1, duration: 1)

                atomsNode_3.hidden = false
                bondsNode_3.hidden = false

                hideTransients(true)

                atomsNode_3.runAction(moveWaterBack)
                bondsNode_3.runAction(moveWaterBack,
                    completionHandler: {
                        self.atomsNode_2.runAction(moveValineBack) ;
                        self.bondsNode_2.runAction(moveValineBack) ;
                        self.atomsNode_3.removeFromParentNode() ;
                        self.bondsNode_3.removeFromParentNode() ;
                        hideTransients(false)
                    })
            }

The hideTransients()) call unhides five nodes, accessing them by name. Sometimes completion hangs, sometimes it doesn't. However if it is hanging in a particular session of remaining on the same page, it will continue to hang on subsequent button toggling. I haven't figured out any cause-effect relationship. I have changed the removeFromParentNode() calls to hidden = true and this currently is working but makes me nervous to rely on.

What on Earth could be happening here?

edit 1: Although it's been working pretty consistently, I have seen the hang-ups a couple times (out of many) using the latter code mentioned so this is not strictly due to the removeFromParentNode() calls.

bpedit
  • 1,176
  • 8
  • 22
  • I'm having a similar issue which I've reported here: https://stackoverflow.com/questions/56189836/in-scenekit-scnaction-hangs-when-called-from-completion-handler-of-runaction – Bbx May 17 '19 at 15:55

1 Answers1

1

Invoking SCNNode.removeFromParentNode() within an action seems fishy to me. I have seen one other case where that seemed to be causing erratic behavior, although there's no documentation that you shouldn't do it.

I think the existence of SCNAction.removeFromParentNode() is a pretty big hint, and invoking that action would be preferable to directly manipulating the node tree.

Using SCNAction.group() and SCNAction.sequence() would make your code a bit simpler.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • As far as I know `runAction()` only acts on a single node. My handler here is acting on 4 different objects so grouping won't work. Similar case using `SCNAction.sequence()`. Once the first pair of simultaneous actions are done, I need to run all those in the handler, different objects again, at the same time. Am I missing something? (BTW, there are reasons the atoms and bonds are kept separate.) Thanks, Hal. – bpedit Mar 12 '16 at 00:43