0

I would like to animate the appearance of a NSSplitViewItem using .setPosition() using Swift, Cocoa and storyboards. My app allows a student to enter a natural deduction proof. When it is not correct, an 'advice view' appears on the right. When it is correct, this advice view will disappear.

The code I'm using is the below, where the first function makes the 'advice' appear, and the second makes it disappear:

  func showAdviceView() {

    // Our window
    let windowSize = view.window?.frame.size.width

    // A CGFloat proportion currently held as a constant
    let adviceViewProportion = BKPrefConstants.adviceWindowSize

    // Position is window size minus the proportion, since
    // origin is top left
    let newPosition = windowSize! - (windowSize! * adviceViewProportion)

    NSAnimationContext.runAnimationGroup { context in
        context.allowsImplicitAnimation = true
        context.duration = 0.75

        splitView.animator().setPosition(newPosition, ofDividerAt: 1)
    }

}

func hideAdviceView() {

    let windowSize = view.window?.frame.size.width
    let newPosition = windowSize!

    NSAnimationContext.runAnimationGroup{ context in
        context.allowsImplicitAnimation = true
        context.duration = 0.75

        splitView.animator().setPosition(newPosition, ofDividerAt: 1)

    }

}

My problem is that the animation action itself is causing the text in the views to stretch, as you can see in this example: Current behaviour

What I really want is the text itself to maintain all proportions and slide gracefully in the same manner that we see when the user themselves moves the separator: Ideal behaviour (but to be achieved programmatically, not manually)

Thus far in my troubleshooting process, I've tried to animate this outside of NSAnimationContext; played with concurrent drawing and autoresizing of subviews in XCode; and looked generally into Cocoa's animation system (though much of what I've read doesn't seem to have direct application here, but I might well be misunderstanding it). I suspect what's going on is that the .animator() proxy object allows only alpha changes and stretches---redrawing so that text alignment is honoured during the animation might be too non-standard. My feeling is that I need to 'trick' the app into treating the animation as though it's being performed by the user, but I'm not sure how to go about that.

Any tips greatly appreciated...

Cheers

  • 1
    Does this answer your question? [How to collapse an NSSplitView pane with animation while using Auto Layout?](https://stackoverflow.com/questions/26664759/how-to-collapse-an-nssplitview-pane-with-animation-while-using-auto-layout) – Willeke Aug 05 '20 at 09:35
  • Thanks, Willeke. I'd reviewed that answer earlier - there is no .isCollapsed method for my view, possibly because my NSSplitView doesn't have an NSSplitViewController, so I can't get an NSSplitViewItem, it appears. Adding a NSSplitViewController seemed a bit overkill the last time I tried to make it work in IB - but that might well be the best solution. – Ian Hocking Aug 05 '20 at 13:32

0 Answers0