在 NSScrollView 中绘制自定义 NSScroller 问题

发布于 2024-10-13 18:29:12 字数 7854 浏览 3 评论 0 原文

我正在尝试绘制一个自定义 NSScroller,并且几乎得到了我想要的所有内容,但是,当我尝试摆脱 KnobSlot 时,它只会不断地重新绘制旋钮:

alt text

那是我使用drawRect:绘制背景的时候。当我删除drawRect时:我得到:

我似乎仍然无法摆脱knobSlot。这是我的全部代码:

    #import "UIScroller.h"

@interface UIScroller (Private)
- (void)drawKnobSlot;
@end

@implementation UIScroller

- (BOOL)isVertical {
    NSRect bounds = [self bounds];
    return NSHeight(bounds) < NSWidth(bounds);
}

+ (CGFloat)scrollerWidth {
    return 13;
}

+ (CGFloat)scrollerWidthForControlSize:(NSControlSize)controlSize {
    return 13;
}

/*
- (void)drawRect:(NSRect)aRect;
{
 //[[NSColor blueColor] set];
 NSRectFill([self bounds]);

  [self drawKnobSlot];
}
 */

- (void)drawKnobSlot;
{
 NSRect slotRect = [self rectForPart:NSScrollerKnobSlot];

 if ([self isVertical])
  NSDrawThreePartImage(slotRect, [NSImage imageNamed:@"lazyarrow"], [NSImage imageNamed:@"lazyarrow"], [NSImage imageNamed:@"lazyarrow"], YES, NSCompositeSourceOver, 1, NO);
 else
  NSDrawThreePartImage(slotRect, [NSImage imageNamed:@"lazyarrow"], [NSImage imageNamed:@"lazyarrow"], [NSImage imageNamed:@"lazyarrow"], NO, NSCompositeSourceOver, 1, NO);
}

- (void)drawPart:(NSScrollerPart)part highlight:(BOOL)highlight {
    //NSLog(@"drawPart:%@ highlight:%@", NSScrollerPartDescription(part), highlight ? @"YES":@"NO");

 NSRect partRect = [self rectForPart:part];

    switch (part) {
        case NSScrollerKnob: {
            assert(!highlight); 

            if ([self isVertical]) {
                partRect.origin.y++;
                partRect.size.height -= 2;
            } else {
                // FIXME really vertical
                partRect.origin.x++;
                partRect.size.width -= 2;
            }


   NSRect newRect = NSMakeRect(partRect.origin.x+4, [self isVertical] ? partRect.origin.y+3 : partRect.origin.y, partRect.size.width - 6,[self isVertical] ? partRect.size.height-5 : partRect.size.height - 4);
   NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:newRect 
                 xRadius:[self isVertical] ? 4: 4
                 yRadius:[self isVertical] ? 4 : 4];

   [path setLineWidth:2];
   [[NSColor colorWithDeviceRed:0.114 green:0.114 blue:0.114 alpha:.6] set];
   [path fill];
   [[NSColor colorWithDeviceWhite:1.0 
          alpha:0.100] set];

   [path addClip];
   [path stroke];

      //      NSBezierPath *knobPath = [NSBezierPath bezierPathWithRoundedRect:partRect
         //                                                            xRadius:5
            //                                                         yRadius:5];

            //[[NSColor colorWithDeviceRed:89./255. green:105./255. blue:131./255. alpha:1.] set];
  /* 
   NSGradient *gradient = [[[NSGradient alloc] initWithColorsAndLocations:
          [NSColor colorWithDeviceRed:(89.0/255.0) green:(105/255.0) blue:(131/255.0) alpha:1.0], 0.0,
          [NSColor colorWithDeviceRed:(89.0/255.0) green:(105/255.0) blue:(131/255.0) alpha:1.0], 0.0768,
          [NSColor colorWithDeviceRed:(154.0/255.0) green:(169/255.0) blue:(190/255.0) alpha:1.0], 0.0769,
          [NSColor colorWithDeviceRed:(94.0/255.0) green:(113/255.0) blue:(144/255.0) alpha:1.0], 0.9231,
          [NSColor colorWithDeviceRed:(61.0/255.0) green:(80/255.0) blue:(111/255.0) alpha:1.0], 0.9232,
          [NSColor colorWithDeviceRed:(61.0/255.0) green:(80/255.0) blue:(111/255.0) alpha:1.0], 1.0,
          nil
          ] autorelease];
   */
   //            [knobPath setLineWidth:2.0];
   //            [knobPath stroke];
   //[gradient drawInBezierPath:knobPath
   //      angle:[self isVertical] ? 90. : 0.];

            //--

            /*[[NSColor colorWithDeviceRed:0. green:0. blue:255/128 alpha:1.0] set];
    [[NSBezierPath bezierPathWithRoundedRect:partRect
    xRadius:6.0
    yRadius:6.0] fill];*/
            //NSRectFill(partRect);
        }   break;
        case NSScrollerIncrementLine: {
 }   break;
        case NSScrollerKnobSlot: {
            NSAssert(!highlight, nil);
#if 0
            [[NSColor darkGrayColor] set];
         //   NSRectFill(partRect);
#else
            partRect = [self bounds];

   /*
   NSGradient *gradient = [[[NSGradient alloc] initWithColorsAndLocations:
          [NSColor colorWithDeviceRed:(89.0/255.0) green:(105/255.0) blue:(131/255.0) alpha:1.0], 0.0,
          [NSColor colorWithDeviceRed:(89.0/255.0) green:(105/255.0) blue:(131/255.0) alpha:1.0], 0.0768,
          [NSColor colorWithDeviceRed:(154.0/255.0) green:(169/255.0) blue:(190/255.0) alpha:1.0], 0.0769,
          [NSColor colorWithDeviceRed:(94.0/255.0) green:(113/255.0) blue:(144/255.0) alpha:1.0], 0.9231,
          [NSColor colorWithDeviceRed:(61.0/255.0) green:(80/255.0) blue:(111/255.0) alpha:1.0], 0.9232,
          [NSColor colorWithDeviceRed:(61.0/255.0) green:(80/255.0) blue:(111/255.0) alpha:1.0], 1.0,
          nil
          ] autorelease];
    */

            NSGradient *gradient = [[[NSGradient alloc] initWithColorsAndLocations:
          [NSColor colorWithDeviceRed:(161.0/255.0) green:(161/255.0) blue:(161/255.0) alpha:1.0], 0.0,
          [NSColor colorWithDeviceRed:(186.0/255.0) green:(186/255.0) blue:(186/255.0) alpha:1.0], 0.067,
          [NSColor colorWithDeviceRed:(219.0/255.0) green:(219/255.0) blue:(219/255.0) alpha:1.0], 0.2,
          [NSColor colorWithDeviceRed:(230.0/255.0) green:(230/255.0) blue:(230/255.0) alpha:1.0], 0.333,
          [NSColor colorWithDeviceRed:(240.0/255.0) green:(240/255.0) blue:(240/255.0) alpha:1.0], 0.667,
          [NSColor colorWithDeviceRed:(223.0/255.0) green:(223/255.0) blue:(223/255.0) alpha:1.0], 0.8,
          [NSColor colorWithDeviceRed:(204.0/255.0) green:(204/255.0) blue:(204/255.0) alpha:1.0], 0.867,
          [NSColor colorWithDeviceRed:(178.0/255.0) green:(178/255.0) blue:(178/255.0) alpha:1.0], 1.0,
          nil
          ] autorelease];


  // CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
  // CGContextSetRGBFillColor(context, 0,0,0,0);
  // CGContextFillRect(context, partRect);
            [gradient drawInRect:partRect angle:[self isVertical] ? 90. : 0.];
  // NSRectFillUsingOperation(partRect, NSCompositeClear);
#endif
        }   

 }}

- (void)drawKnob
{
 [self drawPart:NSScrollerKnob highlight:NO];
 [self setArrowsPosition:NSScrollerArrowsNone];
 /*
 NSRect knobRect = [self rectForPart:NSScrollerKnob];
 NSRect newRect = NSMakeRect(knobRect.origin.x+3, knobRect.origin.y, knobRect.size.width - 6, knobRect.size.height);
 NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:newRect 
               xRadius:5 
               yRadius:5];

 [path setLineWidth:2];
 [[NSColor colorWithDeviceRed:0.114 green:0.114 blue:0.114 alpha:0.3] set];
 [path fill];
 [[NSColor colorWithDeviceWhite:1.0 
        alpha:0.100] set];

 [path addClip];
 [path stroke];


  */
}

- (void)drawKnobSlotInRect:(NSRect)slotRect highlight:(BOOL)flag;
{
 [self drawPart:NSScrollerKnobSlot highlight:NO];
 /*
 NSRect newRect = NSMakeRect(slotRect.origin.x, slotRect.origin.y, slotRect.size.width, slotRect.size.height);
 NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:newRect 
               xRadius:10 
               yRadius:10];

 [path setLineWidth:2];
 [[NSColor colorWithDeviceRed:0.114 green:0.114 blue:0.114 alpha:0.3] set];
 [path fill];
 [[NSColor colorWithDeviceWhite:1.0 
        alpha:0.100] set];

 [path addClip];
 [path stroke];
  */
}

@end

任何人都知道如何解决这个问题,以便它最终看起来像 iOS 的 UIScrollView/UIScroller?:

alt text

或类似 Sparrow 的自定义 NSScrollers?:

替代文字

I'm trying to draw a custom NSScroller and I got mostly everything I wanted to work to work but, when I try and get rid of the knobSlot, it will just continuously re-draw the knob:

alt text

That is when I use drawRect: to draw the background out. When I remove the drawRect: I get:

alt text

I still can't seem to get rid of the knobSlot. Here is all my code:

    #import "UIScroller.h"

@interface UIScroller (Private)
- (void)drawKnobSlot;
@end

@implementation UIScroller

- (BOOL)isVertical {
    NSRect bounds = [self bounds];
    return NSHeight(bounds) < NSWidth(bounds);
}

+ (CGFloat)scrollerWidth {
    return 13;
}

+ (CGFloat)scrollerWidthForControlSize:(NSControlSize)controlSize {
    return 13;
}

/*
- (void)drawRect:(NSRect)aRect;
{
 //[[NSColor blueColor] set];
 NSRectFill([self bounds]);

  [self drawKnobSlot];
}
 */

- (void)drawKnobSlot;
{
 NSRect slotRect = [self rectForPart:NSScrollerKnobSlot];

 if ([self isVertical])
  NSDrawThreePartImage(slotRect, [NSImage imageNamed:@"lazyarrow"], [NSImage imageNamed:@"lazyarrow"], [NSImage imageNamed:@"lazyarrow"], YES, NSCompositeSourceOver, 1, NO);
 else
  NSDrawThreePartImage(slotRect, [NSImage imageNamed:@"lazyarrow"], [NSImage imageNamed:@"lazyarrow"], [NSImage imageNamed:@"lazyarrow"], NO, NSCompositeSourceOver, 1, NO);
}

- (void)drawPart:(NSScrollerPart)part highlight:(BOOL)highlight {
    //NSLog(@"drawPart:%@ highlight:%@", NSScrollerPartDescription(part), highlight ? @"YES":@"NO");

 NSRect partRect = [self rectForPart:part];

    switch (part) {
        case NSScrollerKnob: {
            assert(!highlight); 

            if ([self isVertical]) {
                partRect.origin.y++;
                partRect.size.height -= 2;
            } else {
                // FIXME really vertical
                partRect.origin.x++;
                partRect.size.width -= 2;
            }


   NSRect newRect = NSMakeRect(partRect.origin.x+4, [self isVertical] ? partRect.origin.y+3 : partRect.origin.y, partRect.size.width - 6,[self isVertical] ? partRect.size.height-5 : partRect.size.height - 4);
   NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:newRect 
                 xRadius:[self isVertical] ? 4: 4
                 yRadius:[self isVertical] ? 4 : 4];

   [path setLineWidth:2];
   [[NSColor colorWithDeviceRed:0.114 green:0.114 blue:0.114 alpha:.6] set];
   [path fill];
   [[NSColor colorWithDeviceWhite:1.0 
          alpha:0.100] set];

   [path addClip];
   [path stroke];

      //      NSBezierPath *knobPath = [NSBezierPath bezierPathWithRoundedRect:partRect
         //                                                            xRadius:5
            //                                                         yRadius:5];

            //[[NSColor colorWithDeviceRed:89./255. green:105./255. blue:131./255. alpha:1.] set];
  /* 
   NSGradient *gradient = [[[NSGradient alloc] initWithColorsAndLocations:
          [NSColor colorWithDeviceRed:(89.0/255.0) green:(105/255.0) blue:(131/255.0) alpha:1.0], 0.0,
          [NSColor colorWithDeviceRed:(89.0/255.0) green:(105/255.0) blue:(131/255.0) alpha:1.0], 0.0768,
          [NSColor colorWithDeviceRed:(154.0/255.0) green:(169/255.0) blue:(190/255.0) alpha:1.0], 0.0769,
          [NSColor colorWithDeviceRed:(94.0/255.0) green:(113/255.0) blue:(144/255.0) alpha:1.0], 0.9231,
          [NSColor colorWithDeviceRed:(61.0/255.0) green:(80/255.0) blue:(111/255.0) alpha:1.0], 0.9232,
          [NSColor colorWithDeviceRed:(61.0/255.0) green:(80/255.0) blue:(111/255.0) alpha:1.0], 1.0,
          nil
          ] autorelease];
   */
   //            [knobPath setLineWidth:2.0];
   //            [knobPath stroke];
   //[gradient drawInBezierPath:knobPath
   //      angle:[self isVertical] ? 90. : 0.];

            //--

            /*[[NSColor colorWithDeviceRed:0. green:0. blue:255/128 alpha:1.0] set];
    [[NSBezierPath bezierPathWithRoundedRect:partRect
    xRadius:6.0
    yRadius:6.0] fill];*/
            //NSRectFill(partRect);
        }   break;
        case NSScrollerIncrementLine: {
 }   break;
        case NSScrollerKnobSlot: {
            NSAssert(!highlight, nil);
#if 0
            [[NSColor darkGrayColor] set];
         //   NSRectFill(partRect);
#else
            partRect = [self bounds];

   /*
   NSGradient *gradient = [[[NSGradient alloc] initWithColorsAndLocations:
          [NSColor colorWithDeviceRed:(89.0/255.0) green:(105/255.0) blue:(131/255.0) alpha:1.0], 0.0,
          [NSColor colorWithDeviceRed:(89.0/255.0) green:(105/255.0) blue:(131/255.0) alpha:1.0], 0.0768,
          [NSColor colorWithDeviceRed:(154.0/255.0) green:(169/255.0) blue:(190/255.0) alpha:1.0], 0.0769,
          [NSColor colorWithDeviceRed:(94.0/255.0) green:(113/255.0) blue:(144/255.0) alpha:1.0], 0.9231,
          [NSColor colorWithDeviceRed:(61.0/255.0) green:(80/255.0) blue:(111/255.0) alpha:1.0], 0.9232,
          [NSColor colorWithDeviceRed:(61.0/255.0) green:(80/255.0) blue:(111/255.0) alpha:1.0], 1.0,
          nil
          ] autorelease];
    */

            NSGradient *gradient = [[[NSGradient alloc] initWithColorsAndLocations:
          [NSColor colorWithDeviceRed:(161.0/255.0) green:(161/255.0) blue:(161/255.0) alpha:1.0], 0.0,
          [NSColor colorWithDeviceRed:(186.0/255.0) green:(186/255.0) blue:(186/255.0) alpha:1.0], 0.067,
          [NSColor colorWithDeviceRed:(219.0/255.0) green:(219/255.0) blue:(219/255.0) alpha:1.0], 0.2,
          [NSColor colorWithDeviceRed:(230.0/255.0) green:(230/255.0) blue:(230/255.0) alpha:1.0], 0.333,
          [NSColor colorWithDeviceRed:(240.0/255.0) green:(240/255.0) blue:(240/255.0) alpha:1.0], 0.667,
          [NSColor colorWithDeviceRed:(223.0/255.0) green:(223/255.0) blue:(223/255.0) alpha:1.0], 0.8,
          [NSColor colorWithDeviceRed:(204.0/255.0) green:(204/255.0) blue:(204/255.0) alpha:1.0], 0.867,
          [NSColor colorWithDeviceRed:(178.0/255.0) green:(178/255.0) blue:(178/255.0) alpha:1.0], 1.0,
          nil
          ] autorelease];


  // CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
  // CGContextSetRGBFillColor(context, 0,0,0,0);
  // CGContextFillRect(context, partRect);
            [gradient drawInRect:partRect angle:[self isVertical] ? 90. : 0.];
  // NSRectFillUsingOperation(partRect, NSCompositeClear);
#endif
        }   

 }}

- (void)drawKnob
{
 [self drawPart:NSScrollerKnob highlight:NO];
 [self setArrowsPosition:NSScrollerArrowsNone];
 /*
 NSRect knobRect = [self rectForPart:NSScrollerKnob];
 NSRect newRect = NSMakeRect(knobRect.origin.x+3, knobRect.origin.y, knobRect.size.width - 6, knobRect.size.height);
 NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:newRect 
               xRadius:5 
               yRadius:5];

 [path setLineWidth:2];
 [[NSColor colorWithDeviceRed:0.114 green:0.114 blue:0.114 alpha:0.3] set];
 [path fill];
 [[NSColor colorWithDeviceWhite:1.0 
        alpha:0.100] set];

 [path addClip];
 [path stroke];


  */
}

- (void)drawKnobSlotInRect:(NSRect)slotRect highlight:(BOOL)flag;
{
 [self drawPart:NSScrollerKnobSlot highlight:NO];
 /*
 NSRect newRect = NSMakeRect(slotRect.origin.x, slotRect.origin.y, slotRect.size.width, slotRect.size.height);
 NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:newRect 
               xRadius:10 
               yRadius:10];

 [path setLineWidth:2];
 [[NSColor colorWithDeviceRed:0.114 green:0.114 blue:0.114 alpha:0.3] set];
 [path fill];
 [[NSColor colorWithDeviceWhite:1.0 
        alpha:0.100] set];

 [path addClip];
 [path stroke];
  */
}

@end

Anyone know how to fix this problem so it can end up looking like iOS' UIScrollView/UIScroller?:

alt text

or like Sparrows custom NSScrollers?:

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一念一轮回 2024-10-20 18:29:12

看看 BGHUDAppKit,它是一个 OPENSOURCE(很好!:D)框架,适用于黑色面板 (HUD) NSWindow。它还有一个带有自定义滚动条的自定义 NSScrollView,我曾经使用过该源代码来创建我自己的很棒的滚动条。
我认为这对您来说将是一个很好的起点。

googleCode 上的 BGHudAppKit

Take a look at BGHUDAppKit, its an OPENSOURCE ( nice ! :D ) framework that is meant for the black panel (HUD) NSWindow. It also has a custom NSScrollView with custom scrollers, I have used the source once to create my own awesome scrollers.
I think that will be a great starting point for you.

BGHudAppKit on googleCode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文