NSWindow min and max size (Swift 4)

You can force the views to keep a inferred min and max width/height by using AutoLayout, but what if you’re working with several views simultaneously? Wouldn’t it be simpler to have a window min and max width/height? 

Just add the following in your NSWindowController:

override func windowDidLoad() {

        super.windowDidLoad()

        window?.minSize = NSSize(width: 410, height: 600 )

        window?.manSize = NSSize(width: 600, height: 1200

}

 

Questions / comments? I’m at @MarcMasVi 

Marc

Simple Animations on macOS (Swift 3)

If you’ve ever coded for iOS you’ll most likely be familiar with the UIView animateWithDuration function. Wouldn’t it be great if there would be an OSX equivalent?

Well, there sure is, just type the following:

        NSAnimationContext.runAnimationGroup({_in

            //Indicate the duration of the animation

            NSAnimationContext.current().duration = 5.0 

            //What is being animated? In this example I’m making a view transparent

            someView.animator().alphaValue = 0.0 

        }, completionHandler:{ 

            //In here we add the code that should be triggered after the animation completes.

            print(“Animation completed”)

        })

 

That’s it, you’re good to go! Questions / comments? I’m at @MarcMasVi 

 

Marc

 

Mouse Down (NSEvent in Swift 2)

As I continue adding features for my upcoming app I’m starting to work with NSEvents to add actions to user interaction. I’ve been amazed at how fast is to do this in Swift. 

For instance, detecting where the user presses a button in your window, and then converting it to the coordinate system of a view is extremely easy. Just type the following within your NSView class:

override func mouseDown(theEvent : NSEvent) {

        Swift.print(“Original: \(theEvent.locationInWindow)”)

        let convertedPoint = self.convertPoint(theEvent.locationInWindow, fromView: theEvent.window?.contentView)

        Swift.print(“Tuned: \(convertedPoint)”)

 

    }

Easy as pie!

Feedback, send it @MarcMasVi on Twitter.