Skip to content

Commit

Permalink
Support for Swift 3. Thank you pawelkata.
Browse files Browse the repository at this point in the history
  • Loading branch information
briancorbin committed Sep 30, 2016
1 parent db8ed5e commit c853135
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 46 deletions.
7 changes: 7 additions & 0 deletions Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions SwiftRangeSlider copy-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
14 changes: 13 additions & 1 deletion SwiftRangeSlider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

/* Begin PBXFileReference section */
13BCA713F988FE09DF748C40 /* Pods_SwiftRangeSlider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftRangeSlider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5B00DA9C1D9A5FE900A0A8D7 /* SwiftRangeSlider copy-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "SwiftRangeSlider copy-Info.plist"; path = "/Users/gmabber/Dropbox/code/iphone/SwiftRangeSlider/SwiftRangeSlider copy-Info.plist"; sourceTree = "<absolute>"; };
E80671221CF27AFB00EAE8FA /* SwiftRangeSlider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftRangeSlider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E80671271CF27AFB00EAE8FA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E806712D1CF27E3F00EAE8FA /* RangeSlider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RangeSlider.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -60,6 +61,7 @@
E80671231CF27AFB00EAE8FA /* Products */,
6093D5A44E5D451567813780 /* Pods */,
1B58CD645E8610742E795183 /* Frameworks */,
5B00DA9C1D9A5FE900A0A8D7 /* SwiftRangeSlider copy-Info.plist */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -121,12 +123,13 @@
E80671191CF27AFB00EAE8FA /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0730;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Caramel Apps";
TargetAttributes = {
E80671211CF27AFB00EAE8FA = {
CreatedOnToolsVersion = 7.3.1;
DevelopmentTeam = YFLQ5CNLWN;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -218,8 +221,10 @@
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_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -267,8 +272,10 @@
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_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -288,6 +295,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -300,6 +308,7 @@
baseConfigurationReference = F4C9601CE5B09153BABEA21E /* Pods-SwiftRangeSlider.debug.xcconfig */;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -311,6 +320,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -319,6 +329,7 @@
baseConfigurationReference = F945C7875809BC1BDCB729D1 /* Pods-SwiftRangeSlider.release.xcconfig */;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -329,6 +340,7 @@
PRODUCT_BUNDLE_IDENTIFIER = CA.SwiftRangeSlider;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
50 changes: 25 additions & 25 deletions SwiftRangeSlider/RangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,84 +11,84 @@ import QuartzCore


///Class that represents the RangeSlider object.
@IBDesignable public class RangeSlider: UIControl {
@IBDesignable open class RangeSlider: UIControl {

// MARK: - Properties

///The minimum value selectable on the RangeSlider
@IBInspectable public var minimumValue: Double = 0.0 {
@IBInspectable open var minimumValue: Double = 0.0 {
didSet {
updateLayerFrames()
}
}
///The maximum value selectable on the RangeSlider
@IBInspectable public var maximumValue: Double = 1.0 {
@IBInspectable open var maximumValue: Double = 1.0 {
didSet {
updateLayerFrames()
}
}

///The current lower value selected on the RangeSlider
@IBInspectable public var lowerValue: Double = 0.2 {
@IBInspectable open var lowerValue: Double = 0.2 {
didSet {
updateLayerFrames()
}
}

///The current upper value selected on the RangeSlider
@IBInspectable public var upperValue: Double = 0.8 {
@IBInspectable open var upperValue: Double = 0.8 {
didSet {
updateLayerFrames()
}
}

///The color of the track bar outside of the selected range
@IBInspectable public var trackTintColor: UIColor = UIColor(white: 0.9, alpha: 1.0) {
@IBInspectable open var trackTintColor: UIColor = UIColor(white: 0.9, alpha: 1.0) {
didSet {
trackLayer.setNeedsDisplay()
}
}

///The color of the track bar within the selected range
@IBInspectable public var trackHighlightTintColor: UIColor = UIColor(red: 0.0, green: 0.45, blue: 0.94, alpha: 1.0) {
@IBInspectable open var trackHighlightTintColor: UIColor = UIColor(red: 0.0, green: 0.45, blue: 0.94, alpha: 1.0) {
didSet {
trackLayer.setNeedsDisplay()
}
}

///the thickness of the track bar. `0.1` by default.
@IBInspectable public var trackThickness: CGFloat = 0.1 {
@IBInspectable open var trackThickness: CGFloat = 0.1 {
didSet {
updateLayerFrames()
}
}

///The color of the slider buttons. `White` by default.
@IBInspectable public var thumbTintColor: UIColor = UIColor.whiteColor() {
@IBInspectable open var thumbTintColor: UIColor = UIColor.white {
didSet {
lowerThumbLayer.setNeedsDisplay()
upperThumbLayer.setNeedsDisplay()
}
}

///The thickness of the slider buttons border. `0.1` by default.
@IBInspectable public var thumbBorderThickness: CGFloat = 0.1 {
@IBInspectable open var thumbBorderThickness: CGFloat = 0.1 {
didSet {
lowerThumbLayer.setNeedsDisplay()
upperThumbLayer.setNeedsDisplay()
}
}

///Whether or not the slider buttons have a shadow. `true` by default.
@IBInspectable public var thumbHasShadow: Bool = true {
@IBInspectable open var thumbHasShadow: Bool = true {
didSet{
lowerThumbLayer.setNeedsDisplay()
upperThumbLayer.setNeedsDisplay()
}
}

///The curvaceousness of the ends of the track bar and the slider buttons. `1.0` by default.
@IBInspectable public var curvaceousness: CGFloat = 1.0 {
@IBInspectable open var curvaceousness: CGFloat = 1.0 {
didSet {
trackLayer.setNeedsDisplay()
lowerThumbLayer.setNeedsDisplay()
Expand All @@ -107,7 +107,7 @@ import QuartzCore
}

///The frame of the `RangeSlider` instance.
override public var frame: CGRect {
override open var frame: CGRect {
didSet {
updateLayerFrames()
}
Expand Down Expand Up @@ -137,23 +137,23 @@ import QuartzCore

func addContentViews(){
trackLayer.rangeSlider = self
trackLayer.contentsScale = UIScreen.mainScreen().scale
trackLayer.contentsScale = UIScreen.main.scale
layer.addSublayer(trackLayer)

lowerThumbLayer.rangeSlider = self
lowerThumbLayer.contentsScale = UIScreen.mainScreen().scale
lowerThumbLayer.contentsScale = UIScreen.main.scale
layer.addSublayer(lowerThumbLayer)

upperThumbLayer.rangeSlider = self
upperThumbLayer.contentsScale = UIScreen.mainScreen().scale
upperThumbLayer.contentsScale = UIScreen.main.scale
layer.addSublayer(upperThumbLayer)
}

// MARK: Member Functions


///Updates all of the layer frames that make up the `RangeSlider` instance.
public func updateLayerFrames() {
open func updateLayerFrames() {
CATransaction.begin()
CATransaction.setDisableActions(true)
let newTrackDy = (frame.height - frame.height * trackThickness) / 2
Expand All @@ -173,12 +173,12 @@ import QuartzCore
CATransaction.commit()
}

func positionForValue(value: Double) -> Double {
func positionForValue(_ value: Double) -> Double {
return Double(bounds.width - thumbWidth) * (value - minimumValue) /
(maximumValue - minimumValue) + Double(thumbWidth / 2.0)
}

func boundValue(value: Double, toLowerValue lowerValue: Double, upperValue: Double) -> Double {
func boundValue(_ value: Double, toLowerValue lowerValue: Double, upperValue: Double) -> Double {
return min(max(value, lowerValue), upperValue)
}

Expand All @@ -187,8 +187,8 @@ import QuartzCore

- returns: A bool indicating if either of the slider buttons were inside of the `UITouch`.
*/
override public func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
previousLocation = touch.locationInView(self)
override open func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
previousLocation = touch.location(in: self)

if lowerThumbLayer.frame.contains(previousLocation) {
lowerThumbLayer.highlighted = true
Expand All @@ -205,8 +205,8 @@ import QuartzCore
- returns: A bool indicating success.
*/

override public func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
let location = touch.locationInView(self)
override open func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
let location = touch.location(in: self)

let deltaLocation = Double(location.x - previousLocation.x)
let deltaValue = (maximumValue - minimumValue) * deltaLocation / Double(bounds.width - thumbWidth)
Expand All @@ -221,15 +221,15 @@ import QuartzCore
upperValue = boundValue(upperValue, toLowerValue: lowerValue, upperValue: maximumValue)
}

sendActionsForControlEvents(.ValueChanged)
sendActions(for: .valueChanged)

return true
}

/**
Triggers on the end of touch of the `RangeSlider` and sets the button layers `highlighted` property to `false`.
*/
override public func endTrackingWithTouch(touch: UITouch?, withEvent event: UIEvent?) {
override open func endTracking(_ touch: UITouch?, with event: UIEvent?) {
lowerThumbLayer.highlighted = false
upperThumbLayer.highlighted = false
}
Expand Down
26 changes: 13 additions & 13 deletions SwiftRangeSlider/RangeSliderThumbLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ class RangeSliderThumbLayer: CALayer {
}
weak var rangeSlider: RangeSlider?

override func drawInContext(ctx: CGContext) {
override func draw(in ctx: CGContext) {
if let slider = rangeSlider {
let thumbFrame = bounds.insetBy(dx: 2.0, dy: 2.0)
let cornerRadius = thumbFrame.height * slider.curvaceousness / 2
let thumbPath = UIBezierPath(roundedRect: thumbFrame, cornerRadius: cornerRadius)

let shadowColor = UIColor.grayColor()
let shadowColor = UIColor.gray
if (rangeSlider!.thumbHasShadow){
CGContextSetShadowWithColor(ctx, CGSize(width: 0.0, height: 1.0), 1.0, shadowColor.CGColor)
ctx.setShadow(offset: CGSize(width: 0.0, height: 1.0), blur: 1.0, color: shadowColor.cgColor)
}
CGContextSetFillColorWithColor(ctx, slider.thumbTintColor.CGColor)
CGContextAddPath(ctx, thumbPath.CGPath)
CGContextFillPath(ctx)
ctx.setFillColor(slider.thumbTintColor.cgColor)
ctx.addPath(thumbPath.cgPath)
ctx.fillPath()

CGContextSetStrokeColorWithColor(ctx, shadowColor.CGColor)
CGContextSetLineWidth(ctx, (rangeSlider?.thumbBorderThickness)!)
CGContextAddPath(ctx, thumbPath.CGPath)
CGContextStrokePath(ctx)
ctx.setStrokeColor(shadowColor.cgColor)
ctx.setLineWidth((rangeSlider?.thumbBorderThickness)!)
ctx.addPath(thumbPath.cgPath)
ctx.strokePath()

if highlighted {
CGContextSetFillColorWithColor(ctx, UIColor(white: 0.0, alpha: 0.1).CGColor)
CGContextAddPath(ctx, thumbPath.CGPath)
CGContextFillPath(ctx)
ctx.setFillColor(UIColor(white: 0.0, alpha: 0.1).cgColor)
ctx.addPath(thumbPath.cgPath)
ctx.fillPath()
}
}
}
Expand Down
Loading

0 comments on commit c853135

Please sign in to comment.