Swift - How do you tell when a NSScrollView starts to scroll? - swift

Is there a way in Swift to tell when a NSScrollView has begun scrolling.

You can use an observer that listens to the NSScrollViewWillStartLiveScrollNotification [1]
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClass.YourFunctionName), name: NSScrollViewWillStartLiveScrollNotification, object: nil)
Just place your function inside the #Selector()

Related

how to reload NSTableView data from a different ViewController

I'm working on a simple notes app for macOS. I have a home page which is has a NSTableView that displays all your notes, when you click the new note button a new View appears where you can create a new note. Once you click the note it adds the new note to the database and should reload the table view data, but I need to stop the current run and run the program again to see the changes.
I used this post to achieve the same effect on iOS but it seems to not work on MacOS
So how do I adapt:
override func viewDidLoad() {
super.viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}
In the home page VC
and the line:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
Inside of the saveNewNote IBAction to work in macOS? also are you even able to use the NotificationCenter in macOS apps or is it only on iOS?
NSNotificationCenter is part of Foundation framework, so it's definitely available on macOS.
You should be able to use it the same way you've been using it on iOS.
If you have an IBAction called saveNewNote, inside that method you can posy the notification the way you wrote.
In the view controller which owns the table, add the observer like you wrote, and reload the table...
If it doesn't work, we might need some code example of how you set it up on the Mac app the better understand what isn't working.

NotificationCenter observer calls plays multiple videos - swift - programmatically

I have different UIViews one on top of the other; each of them plays a video using AVPlayer
I need each video to be replayed at the end and in order to do so I use this code:
NotificationCenter.default.addObserver(self, selector: #selector(playerDidReachEnd), name: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem)
#objc fileprivate func playerDidReachEnd(){
self.player.seek(to: .zero)
self.player.play()
}
I noticed that when the selector is called, all the other players in the other UIViews start playing as well...
This is strange to me because I've set the object of the observer to be only the self.player.currentItem
How can I make only this AVPlayer play?
The problem is that that code is in every one of those views. So when the notification is posted, all of those views are observers. So they all start playing.

How to detect window resizing in Mac Catalyst?

How can I a get a notification when user resize the window while:
NotificationCenter.default.addObserver(self, selector: #selector(function), name: NSWindowDidResizeNotification, object: nil)
is unavailable in Mac Catalyst
Just like in iOS. Use windowScene(_:didUpdate:interfaceOrientation:traitCollection:) in your window scene delegate, or implement viewWillTransition(to:with:) in your view controllers.

Detect UIViewController changed in Swift

For analytics purposes, I would like to detect whenever the top UIViewController is modified.
I don't want to use inheritance, but rather delegate some event that I can use.
I see that Firebase have some type of mechanism for that but I couldn't figure how exactly.
Something like the following will be great:
NotificationCenter.default.addObserver(self, selector: #selector(topViewModified), name: .TopViewModified, object: nil)
Thanks.

UIKeyboardWillChangeFrameNotification swift implementation

For a long while now, I've been looking for a Swift implementation that utilizes UIKeyboardWillChangeFrameNotification so far I haven't come across anything. I'm trying to get this working so that I can handle the keyboard show and hide in one method as stated in the documentation here and here written in objective-c.
and replace these:
// Register your Notification, To know When Keyboard appears.
NotificationCenter.default.addObserver(self, selector: #selector(myKeyboardWillShowMethod), name: .UIKeyboardWillShow, object: nil)
// Register your Notification, To know When Keyboard hides.
NotificationCenter.default.addObserver(self, selector: #selector(myKeyboardWillHideMethod), name: .UIKeyboardWillHide, object: nil)