项目作者: maximbilan

项目描述 :
Swift Drawing Application Sample
高级语言: Swift
项目地址: git://github.com/maximbilan/iOS-Swift-Drawing-App.git
创建时间: 2015-04-28T15:40:06Z
项目社区:https://github.com/maximbilan/iOS-Swift-Drawing-App

开源协议:MIT License

下载


iOS Swift Drawing Application Sample

A simple example which describes how to draw in the UIView using Swift programming language.
We need to create the class DrawingView inherited from UIView. With the following properties:

  1. var drawColor = UIColor.blackColor() // A color for drawing
  2. var lineWidth: CGFloat = 5 // A line width
  3.  
  4. private var lastPoint: CGPoint! // A point for storing the last position
  5. private var bezierPath: UIBezierPath! // A bezier path
  6. private var pointCounter: Int = 0 // A counter of ponts
  7. private let pointLimit: Int = 128 // A limit of points
  8. private var preRenderImage: UIImage! // A pre-render image

First of all, initialization. We need to create UIBezierPath and set up some properties.

  1. override init(frame: CGRect) {
  2. super.init(frame: frame)
  3.  
  4. initBezierPath()
  5. }
  6.  
  7. required init(coder aDecoder: NSCoder) {
  8. super.init(coder: aDecoder)
  9.  
  10. initBezierPath()
  11. }
  12.  
  13. func initBezierPath() {
  14. bezierPath = UIBezierPath()
  15. bezierPath.lineCapStyle = kCGLineCapRound
  16. bezierPath.lineJoinStyle = kCGLineJoinRound
  17. }

For better performance we will store the bezier path rendering to UIImage, so create the function renderToImage.

  1. func renderToImage() {
  2.  
  3. UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0)
  4. if preRenderImage != nil {
  5. preRenderImage.drawInRect(self.bounds)
  6. }
  7.  
  8. bezierPath.lineWidth = lineWidth
  9. drawColor.setFill()
  10. drawColor.setStroke()
  11. bezierPath.stroke()
  12.  
  13. preRenderImage = UIGraphicsGetImageFromCurrentImageContext()
  14.  
  15. UIGraphicsEndImageContext()
  16. }

And implement the rendering function.

  1. override func drawRect(rect: CGRect) {
  2. super.drawRect(rect)
  3.  
  4. if preRenderImage != nil {
  5. preRenderImage.drawInRect(self.bounds)
  6. }
  7.  
  8. bezierPath.lineWidth = lineWidth
  9. drawColor.setFill()
  10. drawColor.setStroke()
  11. bezierPath.stroke()
  12. }

First, draw the pre-render image and after that render the current bezier path.

Now, main of our application, it’s touch handling.

In touchesBegan function we save the last point and reset the point counter.

  1. override func touchesBegan(touches: Set, withEvent event: UIEvent) {
  2. let touch: AnyObject? = touches.first
  3. lastPoint = touch!.locationInView(self)
  4. pointCounter = 0
  5. }

In touchesMoved function, add a point to the bezier path, increment the point counter and if the point counter equals a point limit, than render the bezier path to UIImage and reset the bezier path. And update the screen.

  1. override func touchesMoved(touches: Set, withEvent event: UIEvent) {
  2. let touch: AnyObject? = touches.first
  3. var newPoint = touch!.locationInView(self)
  4. bezierPath.moveToPoint(lastPoint)
  5. bezierPath.addLineToPoint(newPoint)
  6. lastPoint = newPoint
  7. ++pointCounter
  8. if pointCounter == pointLimit {
  9. pointCounter = 0
  10. renderToImage()
  11. setNeedsDisplay()
  12. bezierPath.removeAllPoints()
  13. }
  14. else {
  15. setNeedsDisplay()
  16. }
  17. }

In touchesEnded function reset the pointer counter, render the bezier path to UIImage, reset the bezier path and update the screen.

  1. override func touchesEnded(touches: Set, withEvent event: UIEvent) {
  2. pointCounter = 0
  3. renderToImage()
  4. setNeedsDisplay()
  5. bezierPath.removeAllPoints()
  6. }

In touchesCancelled function just call touchesEnded.

  1. override func touchesCancelled(touches: Set!, withEvent event: UIEvent!) {
  2. touchesEnded(touches, withEvent: event)
  3. }

For clearing the view we need remove all points from the bezier path, reset the pre-render image and update the display:

  1. func clear() {
  2. preRenderImage = nil
  3. bezierPath.removeAllPoints()
  4. setNeedsDisplay()
  5. }

And for checking lines on the view:

  1. func hasLines() -> Bool {
  2. return preRenderImage != nil || !bezierPath.empty
  3. }

That’s all and we have really simple drawing application written by Swift.