1

Working with my app, i encountered some issues with drawing stuff:

I was wondering why, back to Obj-c, -moveToPoint() and -lineToPoint() were drawing everything with no problem and now, with swift, everything seems the same except for a strange border appearing on my view. Let me explain better:

Imagine that your task is to draw a basic line from A(0,0) to B(10,10)

We all know how to make that in obj-c, but in swift there is something new to me:

    var path : NSBezierPath = NSBezierPath(rect: dirtyRect)
    let color = NSColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    color.set()
    path.moveToPoint(NSPoint(x: 0,y: 0))
    path.lineToPoint(NSPoint(x: 10,y: 10))
    path.lineWidth = 5.0 //Makes the line 5px width, but it even
                         //creates an annoying border along the view
                         //Try it out, i can't figure out how to get rid of it
    path.stroke()
khagler
  • 3,996
  • 29
  • 40
Alberto
  • 4,212
  • 5
  • 22
  • 36

2 Answers2

5

You are initializing the bezier path with a rect, so that rect is part of the path that gets stroked when you call path.stroke(). You can just initialize the path as NSBezierPath().

trilorez
  • 140
  • 1
  • 1
  • 3
-3

I suggest, instead of using NSBezierPath, NSColor, and NSPoint, you should use the updated UIBezierPath, UIColor, and CGPoint respectively.

elito25
  • 624
  • 1
  • 6
  • 14
  • @FoxNos I don't see how that interferes with changing to the swift equivalents. – elito25 Jun 19 '14 at 21:04
  • Although they may be newer than their NS-predecessors, UIBezierPath and UIColor are not the "updated" versions. - They are for a different platform. – Mick MacCallum Jun 19 '14 at 21:26
  • @0x7fffffff Ohh, sorry, I am still learning Swift too. So these only work on iOS devices? – elito25 Jun 19 '14 at 21:28
  • @elito25 That's alright - Correct. Not all, but quite a few classes from OS X that are prefixed with NS have (roughly) equivalent methods on iOS prefixed with "UI" for their inclusion in the UIKit framework. – Mick MacCallum Jun 19 '14 at 21:30
  • 1
    Indeed. They're not newer, just varied according to the needs of the platform. For example, NSColor has much more in it for managing colour spaces. UIColor doesn't have that stuff because the screens of iOS devices are much less varied, so it doesn't need the complexity of the extra colour space management. – Matt Gibson Jun 20 '14 at 07:39