Search code examples
objective-cmacoscore-plotappkit

Core Plot: Cursor does not adjust over CPTGraphHostingView


I use Core Plot in two macOS apps (first one is old, second one is new). Each of them has a CPTGraphHostingView where some plots are displayed. I can click, hold and drag to change the visible area.

In the first app, when I hoover over the plot, the cursor changes to an open hand. It also changes to a closed hand when I drag. This is what I want, and this is the behaviour in all the sample apps of Core Plot.

In the second app, the cursor always stays the same (arrow).

Edit: Here is a screenshot of the view hierarchy in Xcode (views behind the plot view are hidden). There is no view in front of the CPTGraphHostingView, just a few controls are higher in the view hierarchy, but they are positioned around the plot.

view hierachy

I was not able to find any difference between my two apps (or the second app and the example apps) that could cause this. Both apps compile with Xcode 10.1 and Core Plot release-2.3 branch.

What should I look for?


Solution

  • Since initWithFrame of CPTGraphHostingView does not get called in my second app, the hand cursers are always nil and cannot be adjusted. This is from Apple's docs for "Creating a Custom View":

    View instances that are created in Interface Builder don't call initWithFrame: when their nib files are loaded, which often causes confusion. Remember that Interface Builder archives an object when it saves a nib file, so the view instance will already have been created and initWithFrame: will already have been called.

    The awakeFromNib method provides an opportunity to provide initialization of a view when it is created as a result of a nib file being loaded. When a nib file that contains a view object is loaded, each view instance receives an awakeFromNib message when all the objects have been unarchived. This provides the object an opportunity to initialize any attributes that are not archived with the object in Interface Builder.

    So I subclassed CPTGraphHostingView and loaded the cursors from NSCursor in awakeFromNib, using the advice from Eric Skroch:

    MyGraphHostingView.h:

    #import <CorePlot/CorePlot.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface MyGraphHostingView : CPTGraphHostingView
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    MyGraphHostingView.m:

    #import "MyGraphHostingView.h"
    
    @implementation MyGraphHostingView
    
    -(void)awakeFromNib
    {
        [super awakeFromNib];
    
        if (!self.closedHandCursor) {
            self.closedHandCursor  = [NSCursor closedHandCursor];
        }
        if (!self.openHandCursor) {
            self.openHandCursor    = [NSCursor openHandCursor];
        }
        self.allowPinchScaling = YES;
    }
    
    @end