调整窗口大小时绘图会留下痕迹

时间:2014-01-17 20:45:13

标签: objective-c nsview

我正在尝试在自定义视图中绘制一个框。我有盒子角落的分数,它们是缩放的。如果我在盒子的每个顶点放一个小矩形并调整窗口大小,它就能完美地工作。如果我使用[路径描边]在顶点之间绘制线条然后调整窗口大小,我会在窗口调整大小时重新绘制框时弄得一团糟,所以在调整大小的窗口中不是单个框我得到所有重新绘制的框。我还没有找到清除所有中间图纸的自定义视图的方法。任何帮助,将不胜感激。另外,我真的很陌生。

 #import <Cocoa/Cocoa.h>
  @interface PointDisplay : NSView

  {
       NSBezierPath *pathForLine;
       NSMutableArray *pointList;
       float originalWidth;
       float originalHeight;
  }



  @end
  

导入“PointDisplay.h”

  @implementation PointDisplay

  - (id)initWithFrame:(NSRect)frameRect
  {
       self = [super initWithFrame:frameRect];
       if (self)
            {
                  int opts = (NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);
                  NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds]
                                                                      options:opts
                                                                        owner:self
                                                                     userInfo:nil];
                  [self addTrackingArea:area];

                  pathForLine = [[NSBezierPath alloc] init];
                  pointList = [[NSMutableArray alloc] init];

                  NSRect originalRect = [self bounds];
                  originalWidth = originalRect.size.width;
                  originalHeight = originalRect.size.height;
            }

       return self;
  }


  - (void)drawRect:(NSRect)dirtyRect
  {
       [super drawRect:dirtyRect];

       NSRect myRect = [self bounds];

       float widthNow = myRect.size.width;
       float heightNow = myRect.size.height;

       [[NSColor greenColor] set];
       [NSBezierPath fillRect:myRect];

       int rectWidth = 10, rectHeight = 10;
       float lineWidth = 3.0;

       if (pointList.count != 0)
          {
             NSPoint myPoint = [pointList[0] locationInWindow];

             myPoint = [self convertPoint:myPoint fromView:nil];
             myPoint.x = (myPoint.x - rectWidth / 2) * widthNow / originalWidth;
             myPoint.y = (myPoint.y - rectHeight / 2) * heightNow / originalHeight;

             [pathForLine moveToPoint:myPoint];

             NSRect anotherRect = NSMakeRect(myPoint.x, myPoint.y, rectWidth, rectHeight);

            [[NSColor redColor] set];

            [pathForLine setLineWidth:lineWidth + .5 * ((int)lineWidth % 2)];

          //[[NSBezierPath bezierPathWithRect:anotherRect] stroke];
           for (int i = 1; i < pointList.count; i++)
              {
                 myPoint = [pointList[i] locationInWindow];
                 myPoint = [self convertPoint:myPoint fromView:nil];
                 myPoint.x = (myPoint.x - 5) * widthNow / originalWidth;
                 myPoint.y = (myPoint.y - 5) * heightNow / originalHeight;

                 anotherRect = NSMakeRect(myPoint.x, myPoint.y, 10, 10);
                 [pathForLine lineToPoint:myPoint];
                 [pathForLine stroke];
                //[[NSBezierPath bezierPathWithRect:anotherRect] fill];
              }

          }
  }


  #pragma mark Mouse events

  - (void)mouseUp:(NSEvent *)theEvent
  {
     [pointList addObject:theEvent];
     [self setNeedsDisplay:YES];
  }


  - (void)mouseExited:(NSEvent *)theEvent
  {
     [pathForLine closePath];
     [pathForLine stroke];
     [self setNeedsDisplay:YES];  
  }


  @end

0 个答案:

没有答案