Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swift 4 code conversion #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions SmoothScribble.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -264,6 +265,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand All @@ -277,6 +279,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.SmoothScribble;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -288,6 +291,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = uk.co.flexmonkey.SmoothScribble;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -310,6 +314,7 @@
3EA13AA01BEBDA0600E2B70F /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
46 changes: 20 additions & 26 deletions SmoothScribble/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class ViewController: UIViewController
stackView.addArrangedSubview(simpleScribbleView)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
guard let
location = touches.first?.locationInView(self.view) else
location = touches.first?.location(in: self.view) else
{
return
}
Expand All @@ -49,40 +49,34 @@ class ViewController: UIViewController
return
}

if let adjustedLocationInView = touches.first?.locationInView(touchOrigin)
if let adjustedLocationInView = touches.first?.location(in: touchOrigin)
{
hermiteScribbleView.beginScribble(adjustedLocationInView)
simpleScribbleView.beginScribble(adjustedLocationInView)
hermiteScribbleView.beginScribble(point: adjustedLocationInView)
simpleScribbleView.beginScribble(point: adjustedLocationInView)
}
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
{
guard let
touch = touches.first,
coalescedTouches = event?.coalescedTouchesForTouch(touch),
touchOrigin = touchOrigin
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first,
let coalescedTouches = event?.coalescedTouches(for: touch),
let touchOrigin = touchOrigin
else
{
return
}

coalescedTouches.forEach
{
hermiteScribbleView.appendScribble($0.locationInView(touchOrigin))
simpleScribbleView.appendScribble($0.locationInView(touchOrigin))
hermiteScribbleView.appendScribble(point: $0.location(in: touchOrigin))
simpleScribbleView.appendScribble(point: $0.location(in: touchOrigin))
}
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)
{
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
hermiteScribbleView.endScribble()
simpleScribbleView.endScribble()
}

override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?)
{
if motion == UIEventSubtype.MotionShake
override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == UIEventSubtype.motionShake
{
hermiteScribbleView.clearScribble()
simpleScribbleView.clearScribble()
Expand All @@ -92,17 +86,17 @@ class ViewController: UIViewController
override func viewDidLayoutSubviews()
{
stackView.frame = CGRect(x: 0,
y: topLayoutGuide.length,
width: view.frame.width,
height: view.frame.height - topLayoutGuide.length).insetBy(dx: 10, dy: 10)
y: topLayoutGuide.length,
width: view.frame.width,
height: view.frame.height - topLayoutGuide.length).insetBy(dx: 10, dy: 10)

stackView.axis = view.frame.width > view.frame.height
? UILayoutConstraintAxis.Horizontal
: UILayoutConstraintAxis.Vertical
? UILayoutConstraintAxis.horizontal
: UILayoutConstraintAxis.vertical

stackView.spacing = 10

stackView.distribution = UIStackViewDistribution.FillEqually
stackView.distribution = UIStackViewDistribution.fillEqually
}
}

47 changes: 24 additions & 23 deletions SmoothScribble/components/Scribblable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@ class ScribbleView: UIView

required init(title: String)
{
super.init(frame: CGRectZero)
super.init(frame: CGRect.zero)

backgroundLayer.strokeColor = UIColor.darkGrayColor().CGColor
backgroundLayer.strokeColor = UIColor.darkGray.cgColor
backgroundLayer.fillColor = nil
backgroundLayer.lineWidth = 2

drawingLayer.strokeColor = UIColor.blackColor().CGColor
drawingLayer.strokeColor = UIColor.black.cgColor
drawingLayer.fillColor = nil
drawingLayer.lineWidth = 2

layer.addSublayer(backgroundLayer)
layer.addSublayer(drawingLayer)

titleLabel.text = title
titleLabel.textAlignment = NSTextAlignment.Center
titleLabel.textColor = UIColor.blueColor()
titleLabel.textAlignment = NSTextAlignment.center
titleLabel.textColor = UIColor.blue
addSubview(titleLabel)

layer.borderColor = UIColor.blueColor().CGColor
layer.borderColor = UIColor.blue.cgColor
layer.borderWidth = 1

layer.masksToBounds = true
}

required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
Expand All @@ -61,9 +61,9 @@ class ScribbleView: UIView
override func layoutSubviews()
{
titleLabel.frame = CGRect(x: 0,
y: frame.height - titleLabel.intrinsicContentSize().height - 2,
width: frame.width,
height: titleLabel.intrinsicContentSize().height)
y: frame.height - titleLabel.intrinsicContentSize.height - 2,
width: frame.width,
height: titleLabel.intrinsicContentSize.height)
}
}

Expand All @@ -77,28 +77,28 @@ class SimpleScribbleView: ScribbleView, Scribblable
{
simplePath.removeAllPoints()

simplePath.moveToPoint(point)
simplePath.move(to: point)
}

func appendScribble(point: CGPoint)
{
simplePath.addLineToPoint(point)
simplePath.addLine(to: point)

drawingLayer.path = simplePath.CGPath
drawingLayer.path = simplePath.cgPath
}

func endScribble()
{
if let backgroundPath = backgroundLayer.path
{
simplePath.appendPath(UIBezierPath(CGPath: backgroundPath))
simplePath.append(UIBezierPath(cgPath: backgroundPath))
}

backgroundLayer.path = simplePath.CGPath
backgroundLayer.path = simplePath.cgPath

simplePath.removeAllPoints()

drawingLayer.path = simplePath.CGPath
drawingLayer.path = simplePath.cgPath
}

func clearScribble()
Expand All @@ -118,33 +118,34 @@ class HermiteScribbleView: ScribbleView, Scribblable
{
interpolationPoints = [point]
}

func appendScribble(point: CGPoint)
{
interpolationPoints.append(point)

hermitePath.removeAllPoints()
hermitePath.interpolatePointsWithHermite(interpolationPoints)
hermitePath.interpolatePointsWithHermite(interpolationPoints: interpolationPoints)

drawingLayer.path = hermitePath.CGPath
drawingLayer.path = hermitePath.cgPath
}

func endScribble()
{
if let backgroundPath = backgroundLayer.path
{
hermitePath.appendPath(UIBezierPath(CGPath: backgroundPath))
hermitePath.append(UIBezierPath(cgPath: backgroundPath))
}

backgroundLayer.path = hermitePath.CGPath
backgroundLayer.path = hermitePath.cgPath

hermitePath.removeAllPoints()

drawingLayer.path = hermitePath.CGPath
drawingLayer.path = hermitePath.cgPath
}

func clearScribble()
{
backgroundLayer.path = nil
}
}
}

7 changes: 4 additions & 3 deletions SmoothScribble/extensions/UIBezierPathExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension UIBezierPath
func interpolatePointsWithHermite(interpolationPoints : [CGPoint], alpha : CGFloat = 1.0/3.0)
{
guard !interpolationPoints.isEmpty else { return }
self.moveToPoint(interpolationPoints[0])
self.move(to: interpolationPoints[0])

let n = interpolationPoints.count - 1

Expand Down Expand Up @@ -59,7 +59,8 @@ extension UIBezierPath

let controlPoint2 = CGPoint(x: currentPoint.x - mx * alpha, y: currentPoint.y - my * alpha)

self.addCurveToPoint(endPoint, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
self.addCurve(to: endPoint, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
}
}
}
}