From 026d8e5e93df898399a7b733c156c270d5ec7237 Mon Sep 17 00:00:00 2001 From: Abdulhakim Tlimat Date: Fri, 29 Dec 2017 17:02:34 -0500 Subject: [PATCH] swift 4 code conversion swift 4 code conversion --- SmoothScribble.xcodeproj/project.pbxproj | 5 ++ SmoothScribble/ViewController.swift | 46 ++++++++---------- SmoothScribble/components/Scribblable.swift | 47 ++++++++++--------- .../extensions/UIBezierPathExtension.swift | 7 +-- 4 files changed, 53 insertions(+), 52 deletions(-) diff --git a/SmoothScribble.xcodeproj/project.pbxproj b/SmoothScribble.xcodeproj/project.pbxproj index 309b427..ba53655 100644 --- a/SmoothScribble.xcodeproj/project.pbxproj +++ b/SmoothScribble.xcodeproj/project.pbxproj @@ -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; @@ -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; }; @@ -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; }; @@ -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; }; @@ -310,6 +314,7 @@ 3EA13AA01BEBDA0600E2B70F /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/SmoothScribble/ViewController.swift b/SmoothScribble/ViewController.swift index 9dd39f2..9055dc1 100644 --- a/SmoothScribble/ViewController.swift +++ b/SmoothScribble/ViewController.swift @@ -27,10 +27,10 @@ class ViewController: UIViewController stackView.addArrangedSubview(simpleScribbleView) } - override func touchesBegan(touches: Set, withEvent event: UIEvent?) + override func touchesBegan(_ touches: Set, with event: UIEvent?) { guard let - location = touches.first?.locationInView(self.view) else + location = touches.first?.location(in: self.view) else { return } @@ -49,19 +49,16 @@ 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, withEvent event: UIEvent?) - { - guard let - touch = touches.first, - coalescedTouches = event?.coalescedTouchesForTouch(touch), - touchOrigin = touchOrigin + override func touchesMoved(_ touches: Set, with event: UIEvent?) { + guard let touch = touches.first, + let coalescedTouches = event?.coalescedTouches(for: touch), + let touchOrigin = touchOrigin else { return @@ -69,20 +66,17 @@ class ViewController: UIViewController 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, withEvent event: UIEvent?) - { + override func touchesEnded(_ touches: Set, 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() @@ -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 } } diff --git a/SmoothScribble/components/Scribblable.swift b/SmoothScribble/components/Scribblable.swift index e354e19..0c871c2 100644 --- a/SmoothScribble/components/Scribblable.swift +++ b/SmoothScribble/components/Scribblable.swift @@ -29,13 +29,13 @@ 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 @@ -43,16 +43,16 @@ class ScribbleView: UIView 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") @@ -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) } } @@ -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() @@ -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 } -} \ No newline at end of file +} + diff --git a/SmoothScribble/extensions/UIBezierPathExtension.swift b/SmoothScribble/extensions/UIBezierPathExtension.swift index a19978d..d9d551b 100644 --- a/SmoothScribble/extensions/UIBezierPathExtension.swift +++ b/SmoothScribble/extensions/UIBezierPathExtension.swift @@ -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 @@ -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) } } -} \ No newline at end of file +} +