Drawing app Challenge

I have created an outlet to my view in Document.swift. and I call setupPan() in windowControllerDidLoadNib.
In my .xib I have a custom view of class DrawView. My problem is that when i pan in my view, this should create one oval per panning gesture, but my program draws two ovals. Sort of like a mirror image. Any suggestions?

[code]import Cocoa

class DrawView: NSView {

func setupPan() {
    let pan = NSPanGestureRecognizer(target: self, action: Selector("pan:"))
    self.addGestureRecognizer(pan)
}

func pan(pan: NSPanGestureRecognizer){
    let startingPoint: NSPoint = pan.locationInView(self)
    if pan.state.rawValue == 3 {
    let distancePanned: NSPoint = pan.translationInView(self)
    let drawingRect = CGRect(x: startingPoint.x, y: startingPoint.y, width: sqrt((distancePanned.x * distancePanned.x)), height: sqrt((distancePanned.y * distancePanned.y)))
    println("Drawingrect is size \(drawingRect)")
    drawRect(drawingRect)
    }
}

override func drawRect(dirtyRect: NSRect) {
    // Drawing code here.
    NSColor.blackColor().set()
    let ovalPath = NSBezierPath(ovalInRect: dirtyRect)
    ovalPath.stroke()
    setNeedsDisplayInRect(dirtyRect)
}

}
[/code]

[quote] override func drawRect(dirtyRect: NSRect) { // Drawing code here. NSColor.blackColor().set() let ovalPath = NSBezierPath(ovalInRect: dirtyRect) ovalPath.stroke() setNeedsDisplayInRect(dirtyRect) }[/quote]
What is the justification for doing setNeedsDisplayInRect in the drawRect method?

Have tried without setNeedsDisplay, but then nothing gets drawn.

Well, found out what I did wrong. I called drawRect directly, which you’re obviously not supposed to do. So I just delete the drawRect method call and instead just call setNeedsDisplay. I also call the super method from drawRect for good measure.

[code]import Cocoa

class DrawView: NSView {

func setupPan() {
    let pan = NSPanGestureRecognizer(target: self, action: Selector("pan:"))
    self.addGestureRecognizer(pan)
}

func pan(pan: NSPanGestureRecognizer){
    let startingPoint: NSPoint = pan.locationInView(self)
    let distancePanned: NSPoint = pan.translationInView(self)
    
    if pan.state.rawValue == 3 {
    let drawingRect = CGRect(x: startingPoint.x, y: startingPoint.y, width: sqrt((distancePanned.x * distancePanned.x)), height: sqrt((distancePanned.y * distancePanned.y)))
    println("Drawingrect is size \(drawingRect)")
    setNeedsDisplayInRect(drawingRect)
    }
}

override func drawRect(dirtyRect: NSRect) {
    // Drawing code here.
    super.drawRect(dirtyRect)
    NSColor.blackColor().set()
    let ovalPath = NSBezierPath(ovalInRect: dirtyRect)
    ovalPath.stroke()
}

}[/code]