Superimpose a view on top of NSImageView like the controls in quicktime

Discussion in 'Mac Programming' started by isthisonetaken, Jan 28, 2011.

  1. isthisonetaken, Jan 28, 2011
    Last edited: Jan 28, 2011

    isthisonetaken macrumors regular

    Joined:
    Jun 29, 2006
    #1
    Hey guys,

    so I am creating a sort of movie player (it just displays pictures to create the video) and I want to have controls like in quicktime. I have buttons underneath my NSImageView and have them working. What I am looking for now is when the user moves their mouse onto the NSImageView, a view comes up with buttons the user can press to perform the same actions as the controls under the NSImageView.

    If anyone could point me in the right direction that would be great. I'm thinking I need to find a method that will notify when the mouse is over the image view and found mouse:inRect: which I think will take care of that. Although I'm not entirely sure how to get that method to work properly? I'm unsure how to call the method? Do I need to have a timer running that calls that method? There has to be an easier way.

    My next problem is what is the best way to show the controls? Do I create a view in IB and put controls on them and just show/ hide it based on the return from mouse:inRect? Or should I create a new NIB file and load that? If it is the latter, how do I link the buttons in the NIB to the methods that already work? Also, how do I hide the title bar as all I want are buttons surrounded by a rectangle.

    Thanks for any help.

    Dan
     
  2. isthisonetaken thread starter macrumors regular

    Joined:
    Jun 29, 2006
    #2
    I did some more research and found this: http://stackoverflow.com/questions/4639379/how-to-use-nstrackingarea

    I created a new set of files (VideoPlayer.h/ VideoPlayer.m) and had the .h to inherit from NSImageView and than I set the class for my image view in IB to VideoPlayer. After having the viewWillMoveToWindow and just having alerts come up when the mouseEntered and mouseExited events were fired it sort of worked.

    Problems I ran into were that the rectangle was not the actual image view but a smaller rectangle that seemed to be only the top right quarter of the image view. Also, the code I had working before to set images in the image view stopped working...

    Thoughts?
     
  3. ccamelot macrumors member

    ccamelot

    Joined:
    Feb 21, 2010
    Location:
    Poland
    #3
    Check UKIdleTimer

    Try to google UKIdleTimer by Uli Kusterer. It is old (but still fine) piece of code for detecting user activity. Hope it helps.
     
  4. isthisonetaken thread starter macrumors regular

    Joined:
    Jun 29, 2006
    #4
  5. isthisonetaken thread starter macrumors regular

    Joined:
    Jun 29, 2006
    #5
    Time for more updates. I realized I don't actually need a fancy rectangle around the controls, just the controls themselves. Therefore, I just have buttons and right now I have external buttons on the window to show and hide the player controls (which are linked to the same methods as the controls under the player). Everything works great, except for the controls being shown/ hidden with external buttons instead of mouse events.

    Any suggestions on the NSTrackingArea problems?
     
  6. ccamelot macrumors member

    ccamelot

    Joined:
    Feb 21, 2010
    Location:
    Poland
    #6
    Just take a deeper look at UKIdleTimer

    Hi,

    I am using UKIdleTimer for very similar purpose in my video player app. Works exactly the same as control bar in QuickTime player in fullscreen mode. It uses InstallEventLoopIdleTimer function. Idle timers are only called when there is no user activity occuring in the application. This means that the user is not actively clicking/typing, and is also not in the middle of tracking a control, menu, or window. For example, if you want to start your timer 2 seconds after the user stops typing, etc. you would pass 2.0 into "inDelay" parameter. Each time the user types a key (or whatever), this timer is reset.

    It is perfect solution unless you want to detect mouse move above particular area...

    Les
     
  7. isthisonetaken thread starter macrumors regular

    Joined:
    Jun 29, 2006
    #7
    I am looking to track the mouse above a certain area. The NSImageView is only one part of the window, and I only want to track the mouse entered/ exited events for that view, not the entire window. I had a bit of luck with NSTrackingArea, but as I said above, it wasn't working quite right.
     
  8. ccamelot macrumors member

    ccamelot

    Joined:
    Feb 21, 2010
    Location:
    Poland
    #8
    About NSTrackingArea

    My best bet is you're not setting up your NSTrackingArea properly. It has to be inited with a rectangle. Reason why your trackingArea is occupying only part of the view can be in bad NSRect used in its init method. Consider this example:
    Code:
    trackingArea = [[NSTrackingArea alloc] initWithRect:here_goes_your_NSRect
                options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow )
                owner:self userInfo:nil];
    next you have to add this tracking area:
    Code:
    [self addTrackingArea:trackingArea];
    and next you just have to add your own code to 'mouseEntered', 'mouseMoved' and 'mouseExited' handlers.

    Sometimes you will need - (void)updateTrackingAreas method to recompute tracking area, but normally it is done by Application Kit automatically.

    all best
    Les
     
  9. isthisonetaken thread starter macrumors regular

    Joined:
    Jun 29, 2006
    #9
    My problem is that I don't know where to call that code? When I tried making a subclass of NSImageView and having that code in the attach to window method, it worked (for a part of the image view) except all the rest of my code stopped working. When I try to add the tracking area in the applicationDidFinishLaunching like this:
    Code:
    NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:[videoPlayer frame] options:NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
    [videoPlayer addTrackingArea:trackingArea];
    doesn't work. Neither if I change trying to add it to window, or self. Any suggestions?
     
  10. isthisonetaken thread starter macrumors regular

    Joined:
    Jun 29, 2006
    #10
    Hey guys, I finally got this all figured out. Turns out Apple has a great example of exactly what I was looking for in one of the samples in the class reference for NSTrackingArea called TrackIt. Basically, I needed to add the tracking area in the awakeFromNib method to make it work properly.

    Thanks for all the suggestions along the way though!

    Dan
     

Share This Page