learning apps development with swift in OS X

I prefer to learn programming with physical books. There are too many books on IOS programming and only one or two on Mac OS.

If I choose books on IOS, is it possible to transpose (not totally I presume) the codes for Mac OS ?

A second question.

I would like create a canvas where to draw shapes and, with the mouse, moving only a selected shape, this done without totally redrawing all the view. This is achieved in Delphi in drawing the lines with a special pen mode (in this case XOR pen mode). Is there the same philosophy with cocoa swift ?

Thanks in advance for answers,


Best regards,

Accepted Reply

Effectively, there are very few books for OSX. And I have not found a good one !


You will learn many things with IOS books :

- Swift

- The general API structure (even if syntax if often a bit different) with most objects pretty similar, and mechanisms for actions, delegates, notification…

- XCode


So, it is no waste of time.


But the architecture of a Mac App is pretty different in general: multi windows, menus versus segueing between views in storyboard. And this point of OSX app architecture is very poorly addressed in the books I got.


Personally, I started learning for IOS.

Then, when developing my first OSX App, I used existing code as template.


For your specific point, I think the solution is to use layers


Here is a short example :

- gesture recognizer (panGesture) was added to movableView in IB

- I extracted from an existing code, hope I did not introduce errors.


class AppDelegate: NSObject, NSApplicationDelegate, NSGestureRecognizerDelegate {

    @IBOutlet weak var window: NSWindow!     // The window created with the app

    @IBOutlet weak var movableView: NSView!     // One view you can move
   
    @IBOutlet weak var underFixedView: NSView!     // a view below, that will not be cleared when movable passes over
   
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        movableView.layer?.backgroundColor = NSColor.red.cgColor

        underFixedView.layer?.backgroundColor = NSColor.green.cgColor
}


    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }


    @IBAction func panView(sender: NSPanGestureRecognizer) {

        let t = sender.translation(in: window.contentView)
        sender.view?.frame = NSOffsetRect(sender.view!.frame, t.x, t.y)
        sender.setTranslation(NSPoint.zero, in: nil) // Sinon, on garde des coordonnées erratiques et l'image part dans tous les sens
    }
   
   
    func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: NSGestureRecognizer) -> Bool {
        return true
    }
   
}

Replies

Effectively, there are very few books for OSX. And I have not found a good one !


You will learn many things with IOS books :

- Swift

- The general API structure (even if syntax if often a bit different) with most objects pretty similar, and mechanisms for actions, delegates, notification…

- XCode


So, it is no waste of time.


But the architecture of a Mac App is pretty different in general: multi windows, menus versus segueing between views in storyboard. And this point of OSX app architecture is very poorly addressed in the books I got.


Personally, I started learning for IOS.

Then, when developing my first OSX App, I used existing code as template.


For your specific point, I think the solution is to use layers


Here is a short example :

- gesture recognizer (panGesture) was added to movableView in IB

- I extracted from an existing code, hope I did not introduce errors.


class AppDelegate: NSObject, NSApplicationDelegate, NSGestureRecognizerDelegate {

    @IBOutlet weak var window: NSWindow!     // The window created with the app

    @IBOutlet weak var movableView: NSView!     // One view you can move
   
    @IBOutlet weak var underFixedView: NSView!     // a view below, that will not be cleared when movable passes over
   
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        movableView.layer?.backgroundColor = NSColor.red.cgColor

        underFixedView.layer?.backgroundColor = NSColor.green.cgColor
}


    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }


    @IBAction func panView(sender: NSPanGestureRecognizer) {

        let t = sender.translation(in: window.contentView)
        sender.view?.frame = NSOffsetRect(sender.view!.frame, t.x, t.y)
        sender.setTranslation(NSPoint.zero, in: nil) // Sinon, on garde des coordonnées erratiques et l'image part dans tous les sens
    }
   
   
    func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: NSGestureRecognizer) -> Bool {
        return true
    }
   
}

I did not read this book yet, but I think that it should be good and it is updated to Swift 4.2.


https://www.hackingwithswift.com/store/hacking-with-macos

Other than digital ibooks via Apple, be wary of -any- dead tree books on Swift. Those tend to be out of date, using older code that forces the new coder to spend more time asking for help on something that they won't use with current tools. Two steps/three...


Some interesting info here h ttps://www.smashingmagazine.com/2017/10/from-ios-to-macos-development/


>cocoa swift


Say what? Not a real thing, I think...pick one.


Check iTunes U in the Store for Apple's ebooks, don't ignore Swift Playgrounds for basic getting started learning, and good luck.

Don't forget to ctrl + drag from the Pan gesture Recogniser to setup the delegate and also use wantsLayer to see the colors.


       movableView.wantsLayer = true
       underFixedView.wantsLayer = true


I wonder, Why do you not place this code in a NSViewController?

Thanks, you're right, but I could not post the whole project.


Why not NSViewcontroller ? That was just an old test code to learn gestures on OSX, not optimal code for sure. In fact, for some gestures, layer are even not needed !

Thanks for your answer.