Skip to content

Commit

Permalink
changed interface like factory style.
Browse files Browse the repository at this point in the history
  • Loading branch information
superbderrick committed Sep 26, 2017
1 parent e24ef09 commit f6c1fc1
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 73 deletions.
20 changes: 20 additions & 0 deletions SummerSlider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
4968C90B1F77AADE00C48278 /* SummerSlider.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4968C8A21F77A88300C48278 /* SummerSlider.framework */; };
4968C90C1F77AADE00C48278 /* SummerSlider.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 4968C8A21F77A88300C48278 /* SummerSlider.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
49A7BA9F1F79298500997EC3 /* SummerSliderTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49A7BA9E1F79298500997EC3 /* SummerSliderTypes.swift */; };
49A7BAA51F7A55EC00997EC3 /* HorizontalSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49A7BAA01F7A55EC00997EC3 /* HorizontalSlider.swift */; };
49A7BAA61F7A55EC00997EC3 /* Slider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49A7BAA11F7A55EC00997EC3 /* Slider.swift */; };
49A7BAA71F7A55EC00997EC3 /* SliderDrawingProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49A7BAA21F7A55EC00997EC3 /* SliderDrawingProtocol.swift */; };
49A7BAA81F7A55EC00997EC3 /* SliderFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49A7BAA31F7A55EC00997EC3 /* SliderFactory.swift */; };
49A7BAA91F7A55EC00997EC3 /* VerticlaSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49A7BAA41F7A55EC00997EC3 /* VerticlaSlider.swift */; };
49CA0E8E1F78CED9008CA1F2 /* HorizontalViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49CA0E8B1F78CED9008CA1F2 /* HorizontalViewController.swift */; };
49CA0E8F1F78CED9008CA1F2 /* VerticalViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49CA0E8C1F78CED9008CA1F2 /* VerticalViewController.swift */; };
49CA0E901F78CED9008CA1F2 /* YoutubeStyleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49CA0E8D1F78CED9008CA1F2 /* YoutubeStyleViewController.swift */; };
Expand Down Expand Up @@ -59,6 +64,11 @@
4968C9051F77AAD300C48278 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
4968C9071F77AAD300C48278 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
49A7BA9E1F79298500997EC3 /* SummerSliderTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SummerSliderTypes.swift; sourceTree = "<group>"; };
49A7BAA01F7A55EC00997EC3 /* HorizontalSlider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HorizontalSlider.swift; sourceTree = "<group>"; };
49A7BAA11F7A55EC00997EC3 /* Slider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Slider.swift; sourceTree = "<group>"; };
49A7BAA21F7A55EC00997EC3 /* SliderDrawingProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderDrawingProtocol.swift; sourceTree = "<group>"; };
49A7BAA31F7A55EC00997EC3 /* SliderFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderFactory.swift; sourceTree = "<group>"; };
49A7BAA41F7A55EC00997EC3 /* VerticlaSlider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VerticlaSlider.swift; sourceTree = "<group>"; };
49CA0E8B1F78CED9008CA1F2 /* HorizontalViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HorizontalViewController.swift; sourceTree = "<group>"; };
49CA0E8C1F78CED9008CA1F2 /* VerticalViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VerticalViewController.swift; sourceTree = "<group>"; };
49CA0E8D1F78CED9008CA1F2 /* YoutubeStyleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YoutubeStyleViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -104,6 +114,11 @@
4968C8A41F77A88400C48278 /* SummerSlider */ = {
isa = PBXGroup;
children = (
49A7BAA01F7A55EC00997EC3 /* HorizontalSlider.swift */,
49A7BAA11F7A55EC00997EC3 /* Slider.swift */,
49A7BAA21F7A55EC00997EC3 /* SliderDrawingProtocol.swift */,
49A7BAA31F7A55EC00997EC3 /* SliderFactory.swift */,
49A7BAA41F7A55EC00997EC3 /* VerticlaSlider.swift */,
49A7BA9E1F79298500997EC3 /* SummerSliderTypes.swift */,
4968C8AD1F77A8DB00C48278 /* Constants.swift */,
4968C8AE1F77A8DB00C48278 /* SummerSlider.swift */,
Expand Down Expand Up @@ -254,8 +269,13 @@
buildActionMask = 2147483647;
files = (
4968C8AF1F77A8DB00C48278 /* Constants.swift in Sources */,
49A7BAA81F7A55EC00997EC3 /* SliderFactory.swift in Sources */,
49A7BAA71F7A55EC00997EC3 /* SliderDrawingProtocol.swift in Sources */,
49A7BA9F1F79298500997EC3 /* SummerSliderTypes.swift in Sources */,
49A7BAA91F7A55EC00997EC3 /* VerticlaSlider.swift in Sources */,
49A7BAA51F7A55EC00997EC3 /* HorizontalSlider.swift in Sources */,
4968C8B01F77A8DB00C48278 /* SummerSlider.swift in Sources */,
49A7BAA61F7A55EC00997EC3 /* Slider.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
91 changes: 91 additions & 0 deletions SummerSlider/HorizontalSlider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Sliders.swift
// SummerSlider
//
// Created by derrick on 26/09/2017.
// Copyright © 2017 SuperbDerrick. All rights reserved.
//

import Foundation


struct HorizontalSlider: SliderDrawingProtocol , DrawAPIProtocol{

var slider: Slider

init(slider: Slider){
self.slider = slider
}

func getDrawingImage(rect : CGRect) -> (UIImage,UIImage) {

// Basic Setup to make a Inner Rect.
let innerRect = rect.insetBy(dx: Constants.INNER_RECT.X, dy: Constants.INNER_RECT.Y)
UIGraphicsBeginImageContextWithOptions(innerRect.size ,false , 0)
let context = UIGraphicsGetCurrentContext()!

// Draw Selection Rect.
drawRect(context, innerRect , slider.iSelectedBarColor.cgColor)

// Make Selection Image.
let selectedSide = UIGraphicsGetImageFromCurrentImageContext()
selectedSide!.resizableImage(withCapInsets: UIEdgeInsets.zero)

// Draw UNSelection Rect.
drawRect(context, innerRect , slider.iUnSelectedBarColor.cgColor)

// Make UNSelection Image.
let unSelectedSide = UIGraphicsGetImageFromCurrentImageContext()
unSelectedSide!.resizableImage(withCapInsets: UIEdgeInsets.zero)

// Draw Selection Image.
selectedSide!.draw(at: CGPoint(x:0,y:0))


// Draw Selection Marks. slider.iMarkColor.cgColor, slider.iMarkPositions, slider.iMarkWidth
drawMarks(context, innerRect, slider.iMarkColor.cgColor , slider.iMarkPositions , slider.iMarkWidth )


let selectedStripSide = UIGraphicsGetImageFromCurrentImageContext()!.resizableImage(withCapInsets: UIEdgeInsets.zero)

// Draw UNSelection Image.
unSelectedSide!.draw(at: CGPoint(x:0,y:0))


drawMarks(context, innerRect, slider.iMarkColor.cgColor, slider.iMarkPositions, slider.iMarkWidth)


let unselectedStripSide = UIGraphicsGetImageFromCurrentImageContext()!.resizableImage(withCapInsets: UIEdgeInsets.zero)

UIGraphicsEndImageContext()

return (selectedStripSide,unselectedStripSide)
}

func drawMarks(_ context: CGContext, _ innerRect: CGRect, _ markColor: CGColor, _ marks: Array<Float>!, _ markWidth: Float) {

for mark in marks {
context.setLineWidth(CGFloat(markWidth))
let markWidth = CGFloat(mark)
let postion:CGFloat! = CGFloat((innerRect.width * markWidth ) / Constants.SLIDER.WHOLE_PERCENT)
context.move(to: CGPoint(x: postion, y: innerRect.height / 2 - 5))
context.addLine(to: CGPoint(x: postion, y: innerRect.height / 2 + 5))
context.setStrokeColor(markColor)
context.strokePath()
}

}

func drawRect(_ context:CGContext ,_ innerRect : CGRect , _ rectColr:CGColor)->Void {
context.setLineCap(CGLineCap.round);
context.setLineWidth(12.0)
context.move(to: CGPoint(x: 6, y: innerRect.height / 2))
context.addLine(to: CGPoint(x: innerRect.width - 10, y: innerRect.height / 2))
context.setStrokeColor(rectColr)
context.strokePath()
}

}



20 changes: 20 additions & 0 deletions SummerSlider/Slider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Slider.swift
// SummerSlider
//
// Created by derrick on 26/09/2017.
// Copyright © 2017 SuperbDerrick. All rights reserved.
//

import Foundation

struct Slider{
var iMarkColor : UIColor
var iSelectedBarColor : UIColor
var iUnSelectedBarColor : UIColor
var iMarkWidth : Float
var iMarkPositions : Array<Float>
var iDrawingMode : DrawingMode
var style: SliderStyle
}

22 changes: 22 additions & 0 deletions SummerSlider/SliderDrawingProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// SliderDrawingProtocol.swift
// SummerSlider
//
// Created by derrick on 26/09/2017.
// Copyright © 2017 SuperbDerrick. All rights reserved.
//

import Foundation

protocol SliderDrawingProtocol {
func getDrawingImage(rect : CGRect)->(UIImage,UIImage)
}

protocol DrawAPIProtocol {
func drawMarks(_ context:CGContext ,_ innerRect : CGRect , _ markColor:CGColor ,_ marks:Array<Float>! ,_ markWidth:Float)->Void
func drawRect(_ context:CGContext ,_ innerRect : CGRect , _ rectColr:CGColor)->Void
}




21 changes: 21 additions & 0 deletions SummerSlider/SliderFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// SliderFactory.swift
// SummerSlider
//
// Created by derrick on 26/09/2017.
// Copyright © 2017 SuperbDerrick. All rights reserved.
//

import Foundation


struct SliderFactory{
static func getSlider(slider: Slider)->SliderDrawingProtocol {
switch slider.style {
case .Horizontal:
return HorizontalSlider(slider: slider)
case .Vertical:
return VerticalSlider(slider: slider)
}
}
}
85 changes: 14 additions & 71 deletions SummerSlider/SummerSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,52 +31,18 @@ import UIKit

override public func draw(_ rect: CGRect) {
super.draw(rect)



// Basic Setup to make a Inner Rect.
let innerRect = rect.insetBy(dx: Constants.INNER_RECT.X, dy: Constants.INNER_RECT.Y)
UIGraphicsBeginImageContextWithOptions(innerRect.size ,false , 0)
let context = UIGraphicsGetCurrentContext()!




// Draw Selection Rect.
SummerSlider.drawRect(context, innerRect , self.selectedBarColor.cgColor)

// Make Selection Image.
let selectedSide = UIGraphicsGetImageFromCurrentImageContext()
selectedSide!.resizableImage(withCapInsets: UIEdgeInsets.zero)

// Draw UNSelection Rect.
SummerSlider.drawRect(context, innerRect , self.unselectedBarColor.cgColor)

// Make UNSelection Image.
let unSelectedSide = UIGraphicsGetImageFromCurrentImageContext()
unSelectedSide!.resizableImage(withCapInsets: UIEdgeInsets.zero)

// Draw Selection Image.
selectedSide!.draw(at: CGPoint(x:0,y:0))

// Draw Selection Marks.
SummerSlider.drawMarks(context, innerRect, self.markColor.cgColor, self.markPositions, self.markWidth)


let selectedStripSide = UIGraphicsGetImageFromCurrentImageContext()!.resizableImage(withCapInsets: UIEdgeInsets.zero)

// Draw UNSelection Image.
unSelectedSide!.draw(at: CGPoint(x:0,y:0))


SummerSlider.drawMarks(context, innerRect, self.markColor.cgColor, self.markPositions, self.markWidth)


let unselectedStripSide = UIGraphicsGetImageFromCurrentImageContext()!.resizableImage(withCapInsets: UIEdgeInsets.zero)

UIGraphicsEndImageContext()

setupTrackRange(selectedStripSide, unselectedStripSide)

var sliders = [Slider]()

sliders.append(Slider(iMarkColor: self.markColor , iSelectedBarColor: self.selectedBarColor , iUnSelectedBarColor: self.unselectedBarColor , iMarkWidth: self.markWidth , iMarkPositions: self.markPositions , iDrawingMode: self.drawingMode , style: SliderStyle.Horizontal))

sliders.append(Slider(iMarkColor: self.markColor , iSelectedBarColor: self.selectedBarColor , iUnSelectedBarColor: self.unselectedBarColor , iMarkWidth: self.markWidth , iMarkPositions: self.markPositions , iDrawingMode: self.drawingMode , style: SliderStyle.Vertical))

let horizontalSlider = SliderFactory.getSlider(slider: sliders[0])

let Image = horizontalSlider.getDrawingImage(rect: rect)

setupTrackRange(Image.0, Image.1)
}

private func setupDefaultValues(){
Expand All @@ -88,33 +54,10 @@ import UIKit

self.drawingMode = DrawingMode.BothSides
}



private func setupTrackRange(_ selectedStripSide:UIImage , _ unselectedStripSide:UIImage) {
self.setMinimumTrackImage(selectedStripSide, for: UIControlState.normal)
self.setMaximumTrackImage(unselectedStripSide, for: UIControlState.normal)
}

private static func drawMarks(_ context:CGContext ,_ innerRect : CGRect , _ markColor:CGColor ,_ marks:Array<Float>! ,_ markWidth:Float) {

for mark in marks {
context.setLineWidth(CGFloat(markWidth))
let markWidth = CGFloat(mark)
let postion:CGFloat! = CGFloat((innerRect.width * markWidth ) / Constants.SLIDER.WHOLE_PERCENT)
context.move(to: CGPoint(x: postion, y: innerRect.height / 2 - 5))
context.addLine(to: CGPoint(x: postion, y: innerRect.height / 2 + 5))
context.setStrokeColor(markColor)
context.strokePath()
}
}

private static func drawRect(_ context:CGContext ,_ innerRect : CGRect , _ rectColr:CGColor) {
context.setLineCap(CGLineCap.round);
context.setLineWidth(12.0)
context.move(to: CGPoint(x: 6, y: innerRect.height / 2))
context.addLine(to: CGPoint(x: innerRect.width - 10, y: innerRect.height / 2))
context.setStrokeColor(rectColr)
context.strokePath()
}


}
4 changes: 2 additions & 2 deletions SummerSlider/SummerSliderTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public enum DrawingMode {
case BothSides
case UnselectedOnly
case SelectedOnly
case WithoutMarks
case WithoutMarks
}

public enum Orientation {
public enum SliderStyle {
case Horizontal
case Vertical
}
26 changes: 26 additions & 0 deletions SummerSlider/VerticlaSlider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Sliders.swift
// SummerSlider
//
// Created by derrick on 26/09/2017.
// Copyright © 2017 SuperbDerrick. All rights reserved.
//

import Foundation


struct VerticalSlider: SliderDrawingProtocol{
var slider: Slider

init(slider: Slider){
self.slider = slider
}

func getDrawingImage(rect: CGRect) -> (UIImage,UIImage) {
return (UIImage(),UIImage())
}

}



0 comments on commit f6c1fc1

Please sign in to comment.