Skip to content

Knob control implemented as UIControl subclass in Swift

License

Notifications You must be signed in to change notification settings

carson-katri/Knob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iOS Knob Control

This is a simple iOS knob control implemented in Swift as a UIControl subclass. It is rotated with a single finger pan and is continuous, meaning you can rotate it 360°.

Knob Illustration

Read about its development at Knob: Subclassing UIControl with Swift.

Usage

Copy Knob.swift into your project. It implements UIControl's Target-Action interface. When rotated it generates UIControlEvents.ValueChanged. Get the current rotation from the value property, expressed as radians in the clockwise direction (0 to 2 * M_PI). Example:

override func viewDidLoad() {
    super.viewDidLoad()
        
    let knob = Knob(frame: CGRect(x: 20, y: 20, width: 100, height: 100))
    self.view.addSubview(knob)
    knob.addTarget(self, action: "knobRotated:", forControlEvents: .ValueChanged)
}

func knobRotated(knob: Knob) {
    print("value: \(knob.value)")
}

tintColor

Use tintColor to change the color of the outline and indicator mark.

Knob with red tintColor

knob.tintColor = UIColor.redColor()

backgroundColor

Use backgroundColor to change the fill color.

Knob with gray backgroundColor

knob.backgroundColor = UIColor(white: 0.9, alpha: 1.0)

enabled

Set enabled to false to disable touch events. The stroke and fill color will be modified subtly to indicate the control is disabled.

Disabled knob

leftKnob.enabled = true
rightKnob.enabled = false

License

Knob is available under the MIT license. See the LICENSE file for more info.

About

Knob control implemented as UIControl subclass in Swift

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages