SetNeedsDisplay没有绘制NSRect

时间:2012-02-20 20:27:00

标签: objective-c xcode macos xcode4 nsrect

我的NSRect是在BrightnessView.m中绘制的,它在一个名为“brightness”的xib上连接到NSView的自定义类。 PanelController连接到另一个名为“controller”的xib,这是滑块与动作的对应位置。 Panel Controller.h没有BrightnessView的插座,因为它没有连接到与视图相同的xib。我只需要一个指向亮度视图的指针,或者它能够工作,而不是一个插座吗? 我添加了一个常规指针,但这不会起作用

// Panel Controller.h连接到xib“Controller”上的文件所有者

#import "BrightnessView.h"

@interface PanelController : NSWindowController
{
   BrightnessView *_myBrightnessView;

}

@property (assign) BrightnessView *BrightnessView;

@end

// Panel Controller.m

@synthesize BrightnessView = _myBrightnessView;
- (IBAction)sliderChange:(id)sender;
{
       [_myBrightnessView.self setNeedsDisplay:YES];
}

// BrightnessView.m连接到xib上的NSView“Brightness”

- (void)drawRect:(NSRect)theRect
        {
            NSLog(@"the view is drawn");
            NSRect contentRect = NSInsetRect([self bounds], 0, 0);
            NSBezierPath *path = [NSBezierPath bezierPath];

            [path moveToPoint:NSMakePoint(_arrowX, NSMaxY(contentRect))];
            [path lineToPoint:NSMakePoint(_arrowX / 2, NSMaxY(contentRect))];
            [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect))];

            NSPoint topRightCorner = NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect));
            [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMaxY(contentRect))
                 controlPoint1:topRightCorner controlPoint2:topRightCorner];

            [path lineToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect))];

            NSPoint bottomRightCorner = NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect));
            [path curveToPoint:NSMakePoint(NSMaxX(contentRect), NSMinY(contentRect))
                 controlPoint1:bottomRightCorner controlPoint2:bottomRightCorner];

            [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect))];

            [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMinY(contentRect))
                 controlPoint1:contentRect.origin controlPoint2:contentRect.origin];

            [path lineToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect))];

            NSPoint topLeftCorner = NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect));
            [path curveToPoint:NSMakePoint(NSMinX(contentRect), NSMaxY(contentRect))
                 controlPoint1:topLeftCorner controlPoint2:topLeftCorner];

            [path lineToPoint:NSMakePoint(_arrowX / 2, NSMaxY(contentRect))];
            [path closePath];

            [the_Color setFill];
            [path fill];

            [NSGraphicsContext saveGraphicsState];

            NSBezierPath *clip = [NSBezierPath bezierPathWithRect:[self bounds]];
            [clip appendBezierPath:path];
            [clip addClip];

            [NSGraphicsContext restoreGraphicsState];
        }


        - (void)setArrowX:(NSInteger)value
        {
            _arrowX = value;
        }

www.mediafire.com/?66q8jv3vly1cch4是这个项目的一个例子,它不会起作用。

1 个答案:

答案 0 :(得分:3)

每次用户移动滑块时,您都会创建一个{strong>新 BrightnessView实例。那是错的。您应该使用现有的BrightnessView实例。您的PanelController需要一个指向现有实例的属性或实例变量。