Permalink
Browse files

Adds ability to draw a point with taps

  • Loading branch information...
DarthMike committed May 29, 2015
1 parent 636644d commit 8a5f9a044a9cf39bf7790165edc28046f4701dd9
Showing with 57 additions and 15 deletions.
  1. +57 −15 FreehandDrawing-iOS/DrawView.swift
@@ -33,6 +33,36 @@ class DrawView : UIView {
// MARK: Drawing a path
private func drawLine(a: CGPoint, b: CGPoint, buffer: UIImage?) -> UIImage {
let image = drawInContext { context in
// Draw the line
self.drawColor.setStroke()
CGContextSetLineWidth(context, self.drawWidth)
CGContextSetLineCap(context, kCGLineCapRound)
CGContextMoveToPoint(context, a.x, a.y)
CGContextAddLineToPoint(context, b.x, b.y)
CGContextStrokePath(context)
}
return image
}
// MARK: Drawing a point
private func drawPoint(at: CGPoint, buffer: UIImage?) -> UIImage {
let image = drawInContext { context in
// Draw the point
self.drawColor.setFill()
let circle = UIBezierPath(arcCenter: at, radius: self.drawWidth / 2.0, startAngle: 0, endAngle: 2 * CGFloat(M_PI), clockwise: true)
circle.fill()
}
return image
}
// MARK: General setup to draw. Reusing a buffer and returning a new one
private func drawInContext(code:(context: CGContextRef) -> Void) -> UIImage {
let size = self.bounds.size
// Initialize a full size image. Opaque because we don't need to draw over anything. Will be more performant.
@@ -46,17 +76,11 @@ class DrawView : UIView {
if let buffer = buffer {
buffer.drawInRect(self.bounds)
}
// Execute draw code
code(context: context)
// Draw the line
self.drawColor.setStroke()
CGContextSetLineWidth(context, self.drawWidth)
CGContextSetLineCap(context, kCGLineCapRound)
CGContextMoveToPoint(context, a.x, a.y)
CGContextAddLineToPoint(context, b.x, b.y)
CGContextStrokePath(context)
// Grab the updated buffer
// Grab updated buffer and return it
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
@@ -65,9 +89,13 @@ class DrawView : UIView {
// MARK: Gestures
private func setupGestureRecognizers() {
// 1. Set up a pan gesture recognizer to track where user moves finger
// Pan gesture recognizer to track lines
let panRecognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
self.addGestureRecognizer(panRecognizer)
// Tap gesture recognizer to track points
let tapRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
self.addGestureRecognizer(tapRecognizer)
}
@objc private func handlePan(sender: UIPanGestureRecognizer) {
@@ -86,6 +114,13 @@ class DrawView : UIView {
}
}
@objc private func handleTap(sender: UITapGestureRecognizer) {
let point = sender.locationInView(self)
if sender.state == .Ended {
self.tapAtPoint(point)
}
}
// MARK: Tracing a line
private func startAtPoint(point: CGPoint) {
@@ -94,13 +129,11 @@ class DrawView : UIView {
private func continueAtPoint(point: CGPoint) {
autoreleasepool {
// 2. Draw the current stroke in an accumulated bitmap
// Draw the current stroke in an accumulated bitmap
// Then replace layer contents with the updated image
self.buffer = self.drawLine(self.lastPoint, b: point, buffer: self.buffer)
// 3. Replace the layer contents with the updated image
self.layer.contents = self.buffer?.CGImage ?? nil
// 4. Update last point for next stroke
self.lastPoint = point
}
}
@@ -109,6 +142,15 @@ class DrawView : UIView {
self.lastPoint = CGPointZero
}
// MARK: Tracking a point
private func tapAtPoint(point: CGPoint) {
autoreleasepool {
self.buffer = self.drawPoint(point, buffer: self.buffer)
self.layer.contents = self.buffer?.CGImage ?? nil
}
}
var drawColor: UIColor = UIColor.blackColor()
var drawWidth: CGFloat = 10.0

0 comments on commit 8a5f9a0

Please sign in to comment.