Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rakaramos/OnOffButton
On & Off by Creativedash
https://github.com/rakaramos/OnOffButton
Last synced: about 1 month ago
JSON representation
On & Off by Creativedash
- Host: GitHub
- URL: https://github.com/rakaramos/OnOffButton
- Owner: rakaramos
- License: mit
- Created: 2014-11-11T00:06:03.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-03-18T12:54:39.000Z (almost 8 years ago)
- Last Synced: 2024-04-24T14:48:50.553Z (9 months ago)
- Language: Swift
- Size: 51.8 KB
- Stars: 414
- Watchers: 12
- Forks: 15
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - OnOffButton - Custom On/Off Animated UIButton, written in Swift. By Creativedash (UI / Button)
- awesome-ios-star - OnOffButton - Custom On/Off Animated UIButton, written in Swift. By Creativedash (UI / Button)
README
Custom On/Off Animated UIButton, written in Swift. By Creativedash
===========[![Version](https://img.shields.io/cocoapods/v/OnOffButton.svg?style=flat)](http://cocoapods.org/pods/OnOffButton)
[![License](https://img.shields.io/cocoapods/l/OnOffButton.svg?style=flat)](http://cocoapods.org/pods/OnOffButton)
[![Platform](https://img.shields.io/cocoapods/p/OnOffButton.svg?style=flat)](http://cocoapods.org/pods/OnOffButton)## About
This control is inspired on this Creative Dash dribbble:
![alt tag](https://d13yacurqjgara.cloudfront.net/users/107759/screenshots/1631598/onoff.gif)## Swift Upgrade
Use tags to fit your Swift version:
Swift 3 => `1.4`
Swift 2.3 => `1.3`
## Installation
### [Carthage]
[Carthage]: https://github.com/Carthage/Carthage
Add the following to your Cartfile:
```
github "rakaramos/OnOffButton"
```Then run `carthage update`.
Follow the current instructions in [Carthage's README][carthage-installation]
for up to date installation instructions.[carthage-installation]: https://github.com/Carthage/Carthage#adding-frameworks-to-an-application
### [CocoaPods]
[CocoaPods]: http://cocoapods.org
Add the following to your [Podfile](http://guides.cocoapods.org/using/the-podfile.html):
```ruby
pod 'OnOffButton'
```You will also need to make sure you're opting into using frameworks:
```ruby
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:
```swift
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](https://cloud.githubusercontent.com/assets/7672056/14966575/95d2de1e-1089-11e6-8e22-6beb549c806b.png)
Configure the properties as you want:
![alt tag](https://cloud.githubusercontent.com/assets/7672056/14966574/95d082cc-1089-11e6-9ef7-8215e390bb19.png)
Create an `IBAction`:
```swift
@IBAction func changeButtonState(sender: OnOffButton) {
sender.checked = !sender.checked
}
```Profit ;)
## License
Released under the MIT license. See the LICENSE file for more info.