I want to implement a function that, when a user is hovering over a specific area, a new view appears with an animation similar to a box. And also, when the user leaves a certain area, the box should go with the animation. This is exactly what you see when you hover the top of the screen in OS X, where the Dock appears and disappears with animation.
However, if I implement a function with animation, it does not work properly when I re-enter a specific area until the animation in mouseExited: complete. Here is my code:
let trackingArea = NSTrackingArea(rect: CGRectMake(0, 0, 120, 300), options: NSTrackingAreaOptions.ActiveAlways | NSTrackingAreaOptions.MouseEnteredAndExited, owner: self, userInfo: nil) underView.addTrackingArea(trackingArea) // underView is the dummy view just to respond to the mouse tracking, since the drawerView frame is changed during the animation; not sure if this is the clean way... override func mouseEntered(theEvent: NSEvent) { let frameAfterVisible = CGRectMake(0, 0, 120, 300) NSAnimationContext.runAnimationGroup({ (context: NSAnimationContext!) in context.duration = 0.6 self.drawerView.animator().frame = frameAfterVisible }, completionHandler: { () -> Void in }) } override func mouseExited(theEvent: NSEvent) { let frameAfterInvisible = CGRectMake(-120, 0, 120, 300) NSAnimationContext.runAnimationGroup({ (context: NSAnimationContext!) in context.duration = 0.6 self.drawerView.animator().frame = frameAfterInvisible }, completionHandler: { () -> Void in }) } // drawerView frame upon launch is (-120, 0, 120, 300), since it is not visible at first
In this code, I drawerView by changing its x position. However, as I said, when you enter the tracking zone and then leave the tracking zone, the box works correctly. But this is not the case if you enter the tracking zone again before the lowering animation is fully completed.
Of course, if I set the animation duration to shorter, for example 0.1 , this rarely happened. But I want to move the view using animation.
I want to make drawerView appear again , even if the view did not end the fade . Is there any practice for this?