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

Dynamic initial value for ACSlider #3

Merged
merged 3 commits into from
Feb 17, 2020
Merged
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
23 changes: 11 additions & 12 deletions ACSlider/Classes/ACSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@
import UIKit


protocol TitleValueProvider {
var title: String? { get set }
var value: CGFloat { get set }
}

@IBDesignable
public final class ACSlider: UIControl {
public final class ACSlider: UIControl, TitleValueProvider {

@IBInspectable var title: String? {
@IBInspectable public var title: String? {
get { return thumbView.title }
set { thumbView.title = newValue }
}
@IBInspectable public var value: CGFloat {
get { return thumbView.value }
set { thumbView.value = newValue }
}
@IBInspectable public var maxValue: CGFloat = 23


// MARK: -

private(set) public var value: CGFloat = 0 {
didSet { thumbView.value = String(format: "%02.0f", value) }
}

private var thumbSize: CGSize {
return thumbView.intrinsicContentSize
}
Expand Down Expand Up @@ -73,11 +77,6 @@ public final class ACSlider: UIControl {
addSubview(thumbView)
}

override public func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
value = CGFloat(Int.random(in: 0..<Int(maxValue)))
}

override public func tintColorDidChange() {
super.tintColorDidChange()
tintColorUpdate()
Expand Down
31 changes: 18 additions & 13 deletions ACSlider/Classes/ThumbView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,38 @@ import UIKit


@IBDesignable
final class ThumbView: UIView {
final class ThumbView: UIView, TitleValueProvider {

var title: String? {
get { return titleLabel.text }
set { titleLabel.text = newValue }
}

var value: String? {
get { return valueLabel.text }
set { valueLabel.text = newValue }
private static let valueFormat = "%02.0f"

private var rawValue: CGFloat = 0

var value: CGFloat {
get { return rawValue }
set {
rawValue = newValue
valueLabel.text = String(format: ThumbView.valueFormat, newValue)
}
}


// MARK: -

private let valueLabel: UILabel = {
let label = UILabel(frame: .zero)
label.textAlignment = .center
label.text = "00"

return label
}()
private let valueLabel = ThumbView.createLabel()

private let titleLabel = ThumbView.createLabel()

private let titleLabel: UILabel = {
private static func createLabel() -> UILabel {
let label = UILabel(frame: .zero)
label.textAlignment = .center

return label
}()
}

private let thumbView: UIView = {
return CircleView(frame: .zero)
Expand Down Expand Up @@ -70,6 +73,8 @@ final class ThumbView: UIView {
addSubview(valueLabel)
addSubview(thumbView)

value = 0

configureLayout()
configureTintColor()

Expand Down
7 changes: 5 additions & 2 deletions Example/ACSlider/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<device id="retina4_0" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -29,6 +29,9 @@
<real key="value" value="23"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="title" value="hours"/>
<userDefinedRuntimeAttribute type="number" keyPath="value">
<real key="value" value="5"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="B5b-VE-y0w">
Expand Down
2 changes: 1 addition & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use_frameworks!

platform :ios, '10.0'

target 'ACSlider_Example' do

Expand Down
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
ACSlider: dfc421d970c21c2546f1cfb3b2d6fa241bca1c43
ACSlider: 073fa27f6e8de51f0e971235ddc65f0dcc3e5cb4

PODFILE CHECKSUM: 9369b59a92972a0bcc86321dd5a8a2e36da80f40
PODFILE CHECKSUM: 0f45066dcfce2e95e72674ebcbff779073e085c0

COCOAPODS: 1.6.0
COCOAPODS: 1.7.1
5 changes: 3 additions & 2 deletions Example/Pods/Local Podspecs/ACSlider.podspec.json

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

6 changes: 3 additions & 3 deletions Example/Pods/Manifest.lock

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

Loading