custom NSSliderCell

前端 未结 4 1581
无人共我
无人共我 2020-12-29 15:08

I\'ve sort of accomplised implementing a custom slider cell that can draw over using images for the scroll bar and knob. The only obstacle that is in the way now is this, wh

相关标签:
4条回答
  • 2020-12-29 15:19

    I am a beginner in Objective-c. I also ran into this problem! Here is the solution to find that I spent two days))) Save and restore GraphicsState:

    [NSGraphicsContext restoreGraphicsState];
    //...
    [leftBarImage drawInRect:leftRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
    //...
    [rightBarImage drawInRect:rightRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
    [NSGraphicsContext saveGraphicsState];
    

    Sorry for bad English.

    0 讨论(0)
  • 2020-12-29 15:21

    Ha, it's another story. No, NSResponder is right and you should remove all 'lockFocus' stuff, however, this issue is a result of the default slider bar drawn by the NSSliderCell somewhere outside of the drawBarInside:flipped:flipped. I have faced this issue not so far ago as well.

    Here is discussion and solution: http://www.cocoabuilder.com/archive/cocoa/177288-preventing-nsslider-bar-from-drawing.html , in short, you can override whole drawCell:inView: or use a "dirty hack trick" with overriding a private method. I personally don't like hacks, but in this case I did

    - (BOOL)_usesCustomTrackImage {
    return YES;
    }
    

    And it solved the problem for me

    0 讨论(0)
  • 2020-12-29 15:31

    Remove all the -lockFocus and -unlockFocus messages. The framework will take care of setting up the drawing context for you before -drawBarInside:flipped: or -drawKnob: are ever sent.

    Also, you shouldn't be creating any objects within a draw method.

    0 讨论(0)
  • 2020-12-29 15:34

    Ok, so it's figured out. apparently the slider was trying to be smart and draw only where the knob has been. so apparently I have to invalidate the rect all the time by overriding setNeedsDisplayInRect in the slider class.

    #import "customSlider.h"
    
    @implementation customSlider
    -(void)setNeedsDisplayInRect:(NSRect)invalidRect{
        [super setNeedsDisplayInRect:[self bounds]];
    }
    @end
    
    0 讨论(0)
提交回复
热议问题