Skip to content

Commit

Permalink
Refactoring code all code
Browse files Browse the repository at this point in the history
  • Loading branch information
Yak0xff committed Nov 23, 2017
1 parent 50518c8 commit e0982b1
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 249 deletions.
16 changes: 10 additions & 6 deletions AnimatedPaths/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -13,10 +18,9 @@
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand Down
34 changes: 19 additions & 15 deletions AnimatedPaths/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9532" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -14,28 +18,25 @@
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="hic-yA-ytg">
<rect key="frame" x="0.0" y="20" width="600" height="44"/>
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<constraints>
<constraint firstAttribute="height" constant="44" id="DcU-ST-XUj"/>
</constraints>
<items>
<barButtonItem title="Replay" id="l0K-l3-84B">
<connections>
<action selector="replayAction:" destination="BYZ-38-t0r" id="KlL-8q-nne"/>
</connections>
</barButtonItem>
<barButtonItem style="plain" systemItem="flexibleSpace" id="Ylx-2F-BQB"/>
<barButtonItem style="plain" id="XDF-ou-h4x">
<segmentedControl key="customView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="bar" selectedSegmentIndex="0" id="S6v-6g-7Iz">
<rect key="frame" x="200" y="7" width="200" height="30"/>
<segmentedControl key="customView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="bar" id="S6v-6g-7Iz">
<rect key="frame" x="16" y="6.5" width="343" height="31"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<segments>
<segment title="Drawing"/>
<segment title="Rectangle"/>
<segment title="Polygon"/>
<segment title="Text"/>
<segment title="Custom"/>
</segments>
<connections>
<action selector="drawingTypeSelector:" destination="BYZ-38-t0r" eventType="valueChanged" id="WpK-RX-UYa"/>
Expand All @@ -46,17 +47,20 @@
</items>
</toolbar>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="hic-yA-ytg" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="LUh-j3-2jp"/>
<constraint firstAttribute="trailing" secondItem="hic-yA-ytg" secondAttribute="trailing" id="OHa-ks-ccF"/>
<constraint firstItem="hic-yA-ytg" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="P8C-6K-yHl"/>
</constraints>
</view>
<connections>
<outlet property="Segment" destination="S6v-6g-7Iz" id="rLD-eX-lmk"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-2" y="268"/>
<point key="canvasLocation" x="-420" y="-36.431784107946029"/>
</scene>
</scenes>
</document>
91 changes: 39 additions & 52 deletions AnimatedPaths/RCAnimatedPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,15 @@ import CoreGraphics



class RCAnimatedPath
{
////////////////////////
// CREATE A SINGLETON //
class RCAnimatedPath{

class var shared: RCAnimatedPath
{
struct Singleton
{
class var shared: RCAnimatedPath{
struct Singleton{
static let instance = RCAnimatedPath()
}
return Singleton.instance
}

// CREATE A SINGLETON //
////////////////////////



var animationLayer: CALayer?
var pathLayer: CAShapeLayer?

Expand All @@ -50,15 +40,13 @@ class RCAnimatedPath



func drawAnimatedCustomPath(in view: UIView, path: CGPath, duration: CFTimeInterval, lineWidth: CGFloat, lineColor: UIColor)
{
func drawAnimatedCustomPath(in view: UIView, path: CGPath, duration: CFTimeInterval, lineWidth: CGFloat, lineColor: UIColor){
self.inputDuration = duration
self.inputLineWidth = lineWidth
self.inputLineColor = lineColor
self.inputPath = path

animationLayer = CALayer()

animationLayer?.frame = CGRect(x: 0, y: 0, width: view.layer.bounds.width, height: view.layer.bounds.height)
view.layer.addSublayer(animationLayer!)
view.clipsToBounds = true
Expand All @@ -67,8 +55,7 @@ class RCAnimatedPath
startAnimation()
}

func drawAnimatedRectanglePath(in view: UIView, duration: CFTimeInterval, lineWidth: CGFloat, lineColor: UIColor)
{
func drawAnimatedRectanglePath(in view: UIView, duration: CFTimeInterval, lineWidth: CGFloat, lineColor: UIColor){
self.inputDuration = duration
self.inputLineWidth = lineWidth
self.inputLineColor = lineColor
Expand All @@ -85,8 +72,7 @@ class RCAnimatedPath
startAnimation()
}

func drawAnimatedPolygonPath(in view: UIView, numberOfSides polygonSidesNumber: Int?, rotationAngle: CGFloat?, polygonCornerRadius: Float?, duration: CFTimeInterval, lineWidth: CGFloat, lineColor: UIColor)
{
func drawAnimatedPolygonPath(in view: UIView, numberOfSides polygonSidesNumber: Int?, rotationAngle: CGFloat?, polygonCornerRadius: Float?, duration: CFTimeInterval, lineWidth: CGFloat, lineColor: UIColor){
self.inputDuration = duration
self.inputLineWidth = lineWidth
self.inputLineColor = lineColor
Expand Down Expand Up @@ -114,8 +100,7 @@ class RCAnimatedPath
startAnimation()
}

func drawAnimatedText(in view: UIView, with text: String, duration: CFTimeInterval, lineWidth: CGFloat, textColor: UIColor, fontName: String?, fontSize: CGFloat?)
{
func drawAnimatedText(in view: UIView, with text: String, duration: CFTimeInterval, lineWidth: CGFloat, textColor: UIColor, fontName: String?, fontSize: CGFloat?){
self.inputText = text
self.inputDuration = duration
self.inputLineWidth = lineWidth
Expand All @@ -138,14 +123,12 @@ class RCAnimatedPath
}


func setupDrawingLayer()
{
func setupDrawingLayer(){
clearLayer()

if let _ = animationLayer
{
let pathRect: CGRect = animationLayer!.bounds

let pathRect: CGRect = animationLayer!.bounds.insetBy(dx: 100.0, dy: 100.0)

let pathShapeLayer = CAShapeLayer()
pathShapeLayer.frame = animationLayer!.bounds
Expand All @@ -163,8 +146,7 @@ class RCAnimatedPath
}
}

func setupTextLayer(in view: UIView)
{
func setupTextLayer(in view: UIView){
clearLayer()

if let _ = animationLayer
Expand Down Expand Up @@ -219,20 +201,34 @@ class RCAnimatedPath

}

private func startAnimation()
{
private func startAnimation(){
pathLayer?.removeAllAnimations()

let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
pathAnimation.duration = inputDuration
pathAnimation.fromValue = 0.0
pathAnimation.toValue = 1.0
pathLayer?.add(pathAnimation, forKey: "strokeEnd")
// let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
// pathAnimation.duration = inputDuration
// pathAnimation.fromValue = 0.0
// pathAnimation.toValue = 1.0
// pathLayer?.add(pathAnimation, forKey: "strokeEnd")



let animation = CABasicAnimation(keyPath: "strokeEnd")

animation.fromValue = 0.0
animation.byValue = 1.0
animation.toValue = 1.0
animation.duration = inputDuration

animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false

pathLayer?.add(animation, forKey: "drawLineAnimation")


}

//freezes the animation until clearLayer() is called
func stopAnimatingWithPause()
{
func stopAnimatingWithPause(){
if let pausedTime = pathLayer?.convertTime(CACurrentMediaTime(), from: nil)
{
pathLayer?.speed = 0.0
Expand All @@ -241,25 +237,19 @@ class RCAnimatedPath
}

//clears the animation layers
func stopAnimatingWithClear()
{
func stopAnimatingWithClear(){
clearLayer()
}

func clearLayer()
{
func clearLayer(){
if let _ = pathLayer
{
pathLayer?.removeFromSuperlayer()
pathLayer = nil
}
}


/// CREATE PATH ///

private func rectanglePath(view: UIView) -> CGPath
{
private func rectanglePath(view: UIView) -> CGPath{
//start point - left-down corner
var point = CGPoint(x: inputLineWidth/2, y: 0)
let path = UIBezierPath()
Expand All @@ -285,8 +275,7 @@ class RCAnimatedPath
return path.cgPath
}

private func polygonPath(view: UIView) -> CGPath
{
private func polygonPath(view: UIView) -> CGPath{
let path = UIBezierPath()

let theta = Float(2.0 * .pi) / Float(inputPolygonSidesNumber)
Expand All @@ -295,8 +284,7 @@ class RCAnimatedPath

var length = squareWidth - Float(inputLineWidth)

if inputPolygonSidesNumber % 4 != 0
{
if inputPolygonSidesNumber % 4 != 0{
length = length * cosf(theta / 2.0) + offset / 2.0
}

Expand All @@ -306,8 +294,7 @@ class RCAnimatedPath
var angle = Float(Double.pi)
path.move(to: point)

for _ in 0 ..< inputPolygonSidesNumber
{
for _ in 0 ..< inputPolygonSidesNumber{

let x = Float(point.x) + (sideLength - offset * 2.0) * cosf(angle)
let y = Float(point.y) + (sideLength - offset * 2.0) * sinf(angle)
Expand Down
Loading

0 comments on commit e0982b1

Please sign in to comment.