13

Ideally I'd need something like UIView.animateWithDuration, however I know that this is not an API that's available on macOS. What's the best way to manipulate the actual frame of an NSView and animate the changes?

I tried going for CABasicAnimation, however that's performed on a CALayer rather than the view itself.

What I need is to animate the actual view frame... (or maybe that's a bad practice and there's an alternative?)

So for that reason, after a bit of digging I found NSAnimationContext. That however seems to simply interpolate / fade between two states which is not the feel I am going for.

Here's a small sample of the code I have:

override func mouseDown(with event: NSEvent) {

    let newSize = NSMakeRect(self.frame.origin.x,
                             self.frame.origin.y - 100,
                             self.frame.width, self.frame.height + 100)

    NSAnimationContext.runAnimationGroup({ (context) -> Void in
        context.duration = 0.5
        self.animator().frame = newSize
        }, completionHandler: {
            Swift.print("completed")
    })
}
Martin Velchevski
  • 874
  • 11
  • 33

1 Answers1

0

About half way down there's a Smooth NSView Scale Animation that seems to be going for what you're looking for. Since you're scaling and keeping the view cantered it also deals with that issue.

https://www.advancedswift.com/nsview-animations-guide/#smooth-nsview-scale-animations

Dharman
  • 30,962
  • 25
  • 85
  • 135
user3589502
  • 51
  • 1
  • 1
  • 3