On & Off by Creativedash
Switch branches/tags
Nothing to show
Clone or download
rakaramos Update to swift 3
Bump cocoapods
Latest commit 3fb5926 Mar 18, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Example Update to swift 3 Mar 18, 2017
OnOffButton.xcodeproj Update to swift 3 Mar 18, 2017
OnOffButton Migrate to Swift 2.3 Sep 18, 2016
Sources Update to swift 3 Mar 18, 2017
.gitignore ignoring XCode data Nov 11, 2014
LICENSE create license Nov 11, 2014
OnOffButton.podspec Update to swift 3 Mar 18, 2017
README.md Update README.md Sep 18, 2016

README.md

Custom On/Off Animated UIButton, written in Swift. By Creativedash

Version License Platform

About

This control is inspired on this Creative Dash dribbble: alt tag

Swift Upgrade

Use tags to fit your Swift version:

Swift 3 => 1.4

Swift 2.3 => 1.3

Installation

Carthage

Add the following to your Cartfile:

github "rakaramos/OnOffButton"

Then run carthage update.

Follow the current instructions in Carthage's README for up to date installation instructions.

CocoaPods

Add the following to your Podfile:

pod 'OnOffButton'

You will also need to make sure you're opting into using frameworks:

use_frameworks!

Then run pod install with CocoaPods 0.36 or newer.

Manually

Just copy the OnOffButton.swift into your project.

Implementation

After the installation, you can use it straight in code or with xib/storyboard.

In code:

class ViewController: UIViewController {
    
    let onOffButton = OnOffButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        onOffButton.frame = CGRect(origin: .zero, size:CGSize(width: 100,height: 100))
        // Adjust properties
        onOffButton.lineWidth = 5
        onOffButton.strokeColor = .whiteColor()
        onOffButton.ringAlpha = 0.3
        onOffButton.addTarget(self, action: #selector(ViewController.didTapOnOffButton), forControlEvents: .TouchUpInside)
        
        view.addSubview(onOffButton)
    }
    
    func didTapOnOffButton() {
        onOffButton.checked = !onOffButton.checked
    }
}

Using @IBDesignables

Set the UIButton class to use OnOffButton:

alt tag

Configure the properties as you want:

alt tag

Create an IBAction:

@IBAction func changeButtonState(sender: OnOffButton) {
    sender.checked = !sender.checked
}

Profit ;)

License

Released under the MIT license. See the LICENSE file for more info.