SwiftyKnob allows you to easily add knobs in your iOS apps. If you are unfamiliar with knobs, they are basically curved progress bars. You can use them to show profile completion, stats or just about anything else!
Here's what it looks like:
Via Cocoapods
pod "SwiftyKnob"
Manually
Just drag the Knob.swift
in SwiftyKnob/Classes/
to your project tree and you're ready to go.
import SwiftyKnob
let knob = Knob(frame: CGRect(x: 8, y: 20, width: 120, height: 120),
borderWidth: 18,
borderColor: UIColor.red,
value: 0.78,
text: "78%",
description: "open rate"
)
view.addSubview(knob)
Simply initialize the Knob
class with the wanted parameters and add it to the view. The knob will animate automatically when needed.
For a detailed description of the Knob
initializer see the following table:
Parameter | Description |
---|---|
frame | The rectangle defining the size and position of the knob |
borderWidth | The width of the knob's border |
borderColor | The color of the above border |
value | The value of the knob. Should be between 0.0 and 1.0 |
text | The main text, displayed at the center of the knob |
description | A smaller description, displayed below the text above |
By default, the knob animates as soon as it's drawn. If you want to control that, you can use the follwing code:
let knob = Knob(frame: CGRect(x: 8, y: 20, width: 120, height: 120),
borderWidth: 18,
borderColor: UIColor.red,
value: 0.78,
text: "78%",
description: "open rate",
autoAnimate: false
)
The last parameter tells SwiftyKnob not to animate the knob when it's drawn. You can then use the following line to fire the animation.
knob.animate()
Note: Calling animate
inside viewDidLoad
will not work. Instead you should call it in viewDidAppear
.
-
Add a
UIView
to your scene -
Make
Knob
the custom class of your view
- Customize your knob using the exposed attributes
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
Thanks to @mikedev for his awesome answer on StackOverflow about CAShapeLayer
and CABasicAnimation
!
SwiftyKnob is available under the MIT License. See the LICENSE file for more info.
If you like what I do, you should follow me on Twitter: @jevinsew ✌️