Skip to content

Commit

Permalink
Minor TouchDrawView improvements
Browse files Browse the repository at this point in the history
- Change base class from UIView to UIImageView
  - This allows for some code to be eliminated
- Add `layoutSubviews` method
  - This is called when rotating the device.
    It redraws the stack so that the drawing
    doesn't skew. Was also investigating using
    `draw()` and `setNeedsDisplay()`.
  • Loading branch information
dehli committed Jun 16, 2018
1 parent d7bdf08 commit 0484dcc
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -34,3 +34,4 @@ Carthage/Build

# Swift Package Manager
.build/
.DS_Store
6 changes: 5 additions & 1 deletion Demo/TouchDrawDemo.xcodeproj/project.pbxproj
Expand Up @@ -136,7 +136,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0920;
LastUpgradeCheck = 0940;
ORGANIZATIONNAME = "Christian Paul Dehli";
TargetAttributes = {
EE43D46C1A14A6F200311C06 = {
Expand Down Expand Up @@ -274,12 +274,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -326,12 +328,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0920"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -56,7 +55,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
58 changes: 14 additions & 44 deletions Sources/TouchDrawView.swift
Expand Up @@ -27,54 +27,19 @@
}

/// A subclass of UIView which allows you to draw on the view using your fingers
open class TouchDrawView: UIView {
open class TouchDrawView: UIImageView {

/// Should be set in whichever class is using the TouchDrawView
open weak var delegate: TouchDrawViewDelegate?

/// Used to register undo and redo actions
fileprivate var touchDrawUndoManager: UndoManager!
fileprivate let touchDrawUndoManager = UndoManager()

/// Used to keep track of all the strokes
internal var stack: [Stroke]!
internal var stack: [Stroke] = []

/// Used to keep track of the current StrokeSettings
fileprivate var settings: StrokeSettings!

fileprivate let imageView = UIImageView()

/// Initializes a TouchDrawView instance
override public init(frame: CGRect) {
super.init(frame: frame)
initialize(frame)
}

/// Initializes a TouchDrawView instance
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize(CGRect.zero)
}

/// Adds the subviews and initializes stack
private func initialize(_ frame: CGRect) {
stack = []
settings = StrokeSettings()

addSubview(imageView)

touchDrawUndoManager = undoManager
if touchDrawUndoManager == nil {
touchDrawUndoManager = UndoManager()
}

// Initially sets the frames of the UIImageView
draw(frame)
}

/// Sets the frames of the subviews
override open func draw(_ rect: CGRect) {
imageView.frame = rect
}
fileprivate var settings = StrokeSettings()

/// Imports the stack so that previously exported stack can be used
open func importStack(_ stack: [Stroke]) {
Expand Down Expand Up @@ -105,8 +70,8 @@ open class TouchDrawView: UIView {

/// Exports the current drawing
open func exportDrawing() -> UIImage {
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, UIScreen.main.scale)
imageView.image?.draw(in: imageView.frame)
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
self.image?.draw(in: frame)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
Expand Down Expand Up @@ -213,6 +178,11 @@ open class TouchDrawView: UIView {
redrawStack()
touchDrawUndoManager.registerUndo(withTarget: self, selector: #selector(clearDrawing), object: nil)
}

open override func layoutSubviews() {
super.layoutSubviews()
redrawStack()
}
}

// MARK: - Touch Actions
Expand Down Expand Up @@ -269,18 +239,18 @@ fileprivate extension TouchDrawView {

/// Begins the image context
func beginImageContext() {
UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, false, UIScreen.main.scale)
UIGraphicsBeginImageContextWithOptions(frame.size, false, UIScreen.main.scale)
}

/// Ends image context and sets UIImage to what was on the context
func endImageContext() {
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}

/// Draws the current image for context
func drawCurrentImage() {
imageView.image?.draw(in: imageView.bounds)
image?.draw(in: bounds)
}

/// Clears view, then draws stack
Expand Down
6 changes: 5 additions & 1 deletion TouchDraw.xcodeproj/project.pbxproj
Expand Up @@ -160,7 +160,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 0940;
ORGANIZATIONNAME = "Christian Paul Dehli";
TargetAttributes = {
89EADBB31CB87C23001130A8 = {
Expand Down Expand Up @@ -278,12 +278,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -334,12 +336,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -56,7 +55,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -10,7 +10,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand All @@ -31,7 +30,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down

0 comments on commit 0484dcc

Please sign in to comment.