Skip to content

A lightweight Swift-based drawing framework using Core Graphics, offering customizable brushes, Apple Pencil support, and undo/redo for iOS 13+.

License

Notifications You must be signed in to change notification settings

jaydeep-godhani/SwiftDrawKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SwiftDrawKit

GitHub repo size GitHub stars GitHub forks Platform Language License

Overview

SwiftDrawKit is a simple, light-weight drawing framework written in Swift. SwiftDrawKit is built using Core Gaphics and is very easy to implement.

Demo

Requirements

  • iOS 13.0+
  • Swift 5
  • Xcode 12 or higher

Installation

This is an Xcode project, so you can directly clone or download the project into your workspace.

Clone the Repository

git clone https://github.com/jaydeep-godhani/SwiftDrawKit.git

Alternatively, you can download the project as a ZIP file from the GitHub repository page.

Integrating into Your Project

To integrate the SwiftDrawView into your own Xcode project:

  1. Download or clone the repository.
  2. Copy the contents of the DrawKit folder into your project.
  3. Add SwiftDrawView to your storyboard or use it programmatically in your view controllers.

Usage

Using SwiftDrawKit is very simple:

Getting Started:

Create a SwiftDrawView and add it to your ViewController:

let drawView = SwiftDrawView(frame: self.view.frame)
self.view.addSubview(drawView)

By default, the view will automatically respond to touch gestures and begin drawing. The default brush is .default, which has a black color.

To disable drawing, simply set the isEnabled property to false:

drawView.isEnabled = false

Brushes

For drawing, we use Brush to keep track of styles like width, color, etc.. We have multiple different default brushes, you can use as follows:

drawView.brush = Brush.default

The default brushed are:

public static var `default`: Brush { get } // black, width 3
public static var thin     : Brush { get } // black, width 2
public static var medium   : Brush { get } // black, width 7
public static var thick    : Brush { get } // black, width 10
public static var marker   : Brush { get } // flat red-ish, width 10
public static var eraser   : Brush { get } // clear, width 8; uses CGBlendMode to erase things

Adjusted Width Factor

SwiftDrawView supports drawing-angle-adjusted brushes. This effectively means, if the user (using an Pencil) draws with the tip of the pencil, the brush will reduce its width a little. If the user draws at a very low angle, with the side of the pencil, the brush will be a little thicker. You can modify this behavior by setting adjustedWidthFactor of a brush. If you increase the number (to, say, 5) the changes will increase. If you reduce the number to 0, the width will not be adjusted at all. The default value is 1 which causes a slight de-/increase in width.

This is an opt-in feature. That means, in shouldBeginDrawingIn, you need to call drawingView.brush.adjustWidth(for: touch).

Further Customization:

For more customization, you can modify the different properties of a brush to fit your needs.

Line Color:

The color of a line stroke can be changed by adjusting the color property of a brush. SwiftDrawKit accepts any Color:

drawView.brush.color = Color(.red)

or

drawView.brush.color = Color(UIColor(colorLiteralRed: 0.75, green: 0.50, blue: 0.88, alpha: 1.0))

We have our own implementation of UIColorColor – to be able to de-/encode it.

Line Width:

The width of a line stroke can be changed by adjusting the width property of a brush. SwiftDrawKit accepts any positive CGFloat:

drawView.brush.width = 5.0

Line Opacity:

The opacity of a line stroke can be changed by adjusting the lineOpacity property. SwiftDrawKit accepts any CGFloat between 0 and 1:

drawView.brush.opacity = 0.5

Editing

Clear All:

If you wish to clear the entire canvas, simply call the clear function:

drawView.clear()

Drawing History:

drawView.undo()

...and redo:

drawView.redo()

To en-/disable custom un- & redo buttons, you can use .canUndo and .canRedo.

Apple Pencil Integration

Apple Pencil can be used for drawing in a SwiftDrawView, just like a finger.
Special features, however, regarding Apple Pencil 2 are only supported on iOS 12.1 and above versions.

Apple Pencil 2 Double Tap action

Enable/ Disable pencil interaction

Apple Pencil interaction is enabled by default, but you can set drawView.isPencilInteractive to change that setting.

Pencil Events

When double tapping the pencil, SwiftDrawKit will check the user preferences set in the system. If the preference is set to switch to eraser, SwiftDrawKit will switch between normal and erasing mode; if set to last used tool, SwiftDrawKit will switch between current and previous brush.

Delegate

SwiftDrawKit has delegate functions to notify you when a user is interacting with a SwiftDrawView. To access these delegate methods, have your View Controller conform to the SwiftDrawViewDelegate protocol:

class ViewController: UIViewController, SwiftDrawViewDelegate

Delegate methods

func swiftDraw(shouldBeginDrawingIn drawingView: SwiftDrawView, using touch: UITouch) -> Bool

func swiftDraw(didBeginDrawingIn drawingView: SwiftDrawView, using touch: UITouch)

func swiftDraw(isDrawingIn drawingView: SwiftDrawView, using touch: UITouch)
    
func swiftDraw(didFinishDrawingIn drawingView: SwiftDrawView, using touch: UITouch)
    
func swiftDraw(didCancelDrawingIn drawingView: SwiftDrawView, using touch: UITouch)

Contributions

We welcome contributions! If you find a bug, have an idea for a new extension, or want to improve the documentation, feel free to fork the repo and create a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A lightweight Swift-based drawing framework using Core Graphics, offering customizable brushes, Apple Pencil support, and undo/redo for iOS 13+.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy