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

Add swift-package support #6

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

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

63 changes: 45 additions & 18 deletions LMGaugeViewSwift/Classes/GaugeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import Foundation
import UIKit

public protocol GaugeViewDelegate: class {
public protocol GaugeViewDelegate: AnyObject {

/// Return ring stroke color from the specified value.
func ringStokeColor(gaugeView: GaugeView, value: Double) -> UIColor
func ringStokeColor(gaugeView: GaugeView, value: Double?) -> UIColor
}

open class GaugeView: UIView {
Expand All @@ -22,24 +22,31 @@ open class GaugeView: UIView {

public static let defaultMinMaxValueFont = "HelveticaNeue"

public static let unitOfMeasurementLabelHeight = 20

/// Current value.
public var value: Double = 0 {
public var value: Double? = nil {
didSet {
value = max(min(value, maxValue), minValue)

// Set text for value label
valueLabel.text = String(format: "%.0f", value)
showValueLabelText()

// Trigger the stoke animation of ring layer
strokeGauge()
}
}

/// Decimal places
public var decimalPlaces: Int = 1 {
didSet {
decimalPlacesFormatString = "%.\(decimalPlaces)f"
}
}

/// Minimum value.
public var minValue: Double = 0

/// Maximum value.
public var maxValue: Double = 120
public var maxValue: Double = 100

/// Limit value.
public var limitValue: Double = 50
Expand Down Expand Up @@ -114,6 +121,7 @@ open class GaugeView: UIView {
var endAngle: Double = .pi/4 + .pi * 2
var divisionUnitAngle: Double = 0
var divisionUnitValue: Double = 0
var decimalPlacesFormatString = "%.1f"

lazy var progressLayer: CAShapeLayer = {
let layer = CAShapeLayer()
Expand All @@ -125,7 +133,7 @@ open class GaugeView: UIView {
return layer
}()

lazy var valueLabel: UILabel = {
lazy public var valueLabel: UILabel = {
let label = UILabel()
label.backgroundColor = UIColor.clear
label.textAlignment = .center
Expand Down Expand Up @@ -250,21 +258,22 @@ open class GaugeView: UIView {
if valueLabel.superview == nil {
addSubview(valueLabel)
}
valueLabel.text = String(format: "%.0f", value)
showValueLabelText()
valueLabel.font = valueFont
valueLabel.minimumScaleFactor = 10/valueFont.pointSize
valueLabel.minimumScaleFactor = 0.5
valueLabel.textColor = valueTextColor
let insetX = ringThickness + divisionsPadding * 2 + divisionsRadius
valueLabel.frame = progressLayer.frame.insetBy(dx: CGFloat(insetX), dy: CGFloat(insetX))
let insetY = showUnitOfMeasurement ? insetX + Double(GaugeView.unitOfMeasurementLabelHeight) : insetX
valueLabel.frame = progressLayer.frame.insetBy(dx: CGFloat(insetX), dy: CGFloat(insetY))
valueLabel.frame = valueLabel.frame.offsetBy(dx: 0, dy: showUnitOfMeasurement ? CGFloat(-divisionsPadding/2) : 0)

// Min Value Label
if minValueLabel.superview == nil {
addSubview(minValueLabel)
}
minValueLabel.text = String(format: "%.0f", minValue)
minValueLabel.text = String(format: decimalPlacesFormatString, minValue)
minValueLabel.font = minMaxValueFont
minValueLabel.minimumScaleFactor = 10/minMaxValueFont.pointSize
minValueLabel.minimumScaleFactor = 0.5
minValueLabel.textColor = minMaxValueTextColor
minValueLabel.isHidden = !showMinMaxValue
let minDotCenter = CGPoint(x: CGFloat(dotRadius * cos(startAngle)) + center.x,
Expand All @@ -275,9 +284,9 @@ open class GaugeView: UIView {
if maxValueLabel.superview == nil {
addSubview(maxValueLabel)
}
maxValueLabel.text = String(format: "%.0f", maxValue)
maxValueLabel.text = String(format: decimalPlacesFormatString, maxValue)
maxValueLabel.font = minMaxValueFont
maxValueLabel.minimumScaleFactor = 10/minMaxValueFont.pointSize
maxValueLabel.minimumScaleFactor = 0.5
maxValueLabel.textColor = minMaxValueTextColor
maxValueLabel.isHidden = !showMinMaxValue
let maxDotCenter = CGPoint(x: CGFloat(dotRadius * cos(endAngle)) + center.x,
Expand All @@ -290,19 +299,24 @@ open class GaugeView: UIView {
}
unitOfMeasurementLabel.text = unitOfMeasurement
unitOfMeasurementLabel.font = unitOfMeasurementFont
unitOfMeasurementLabel.minimumScaleFactor = 10/unitOfMeasurementFont.pointSize
unitOfMeasurementLabel.minimumScaleFactor = 0.5
unitOfMeasurementLabel.textColor = unitOfMeasurementTextColor
unitOfMeasurementLabel.isHidden = !showUnitOfMeasurement
unitOfMeasurementLabel.frame = CGRect(x: valueLabel.frame.origin.x,
y: valueLabel.frame.maxY - 10,
width: valueLabel.frame.width,
height: 20)
height: CGFloat(GaugeView.unitOfMeasurementLabelHeight))
}

public func strokeGauge() {

// Set progress for ring layer
let progress = maxValue != 0 ? (value - minValue)/(maxValue - minValue) : 0
let progress: Double
if let value = value {
progress = maxValue != 0 ? (value - minValue)/(maxValue - minValue) : 0
} else {
progress = 0
}
progressLayer.strokeEnd = CGFloat(progress)

// Set ring stroke color
Expand All @@ -312,6 +326,19 @@ open class GaugeView: UIView {
}
progressLayer.strokeColor = ringColor.cgColor
}


private func showValueLabelText() {
if let value = value {
if value.isNaN {
valueLabel.text = "-"
} else {
valueLabel.text = String(format: decimalPlacesFormatString, value)
}
} else {
valueLabel.text = "-"
}
}
}

// MARK: - SUPPORT
Expand Down
30 changes: 30 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "LMGaugeViewSwift",
platforms: [
.iOS(.v11)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "LMGaugeViewSwift",
targets: ["LMGaugeViewSwift"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "LMGaugeViewSwift",
dependencies: [],
path: "LMGaugeViewSwift/Classes"
)
]
)
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# LMGaugeViewSwift

[![CI Status](https://img.shields.io/travis/LMinh/LMGaugeViewSwift.svg?style=flat)](https://travis-ci.org/LMinh/LMGaugeViewSwift)
![Swift-Package](https://img.shields.io/badge/Swift--Package-1.1.0-brightgreen)
[![Version](https://img.shields.io/cocoapods/v/LMGaugeViewSwift.svg?style=flat)](https://cocoapods.org/pods/LMGaugeViewSwift)
[![License](https://img.shields.io/cocoapods/l/LMGaugeViewSwift.svg?style=flat)](https://cocoapods.org/pods/LMGaugeViewSwift)
[![Platform](https://img.shields.io/cocoapods/p/LMGaugeViewSwift.svg?style=flat)](https://cocoapods.org/pods/LMGaugeViewSwift)
Expand All @@ -19,14 +20,25 @@ https://github.com/lminhtm/LMGaugeView
## Requirements
* iOS 8.0 or higher

## Installation
## CocoaPods Installation
LMGaugeViewSwift is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:

```ruby
pod 'LMGaugeViewSwift'
```

## Swift-Package Installation
Open your project in Xcode

1. Click "File" -> "Add Packages..."
2. Paste the following URL: https://github.com/gallinaettore/LMGaugeViewSwift

You can specify the dependency in Package.swift by adding this:
```ruby
.package(url: "https://github.com/gallinaettore/LMGaugeViewSwift.git", .upToNextMajor(from: "1.1.0"))
```

## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.

Expand Down