OnOffButton
  • November 10, 2023

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


About


This control is inspired on this Creative Dash dribbble: 

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:

Configure the properties as you want:

Create an IBAction:

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

Profit 😉

GitHub


View Github

#carthage #cocoapods #framework #ios #storyboard #swift #uibutton
YOU MIGHT ALSO LIKE...
🧭 NavigationKit

NavigationKit is a lightweight library which makes SwiftUI navigation super easy to use. 💻 Installation 📦 Swift Package Manager Using Swift Package Manager, add ...

swiftui-navigation-stack

An alternative SwiftUI NavigationView implementing classic stack-based navigation giving also some more control on animations and programmatic navigation. NavigationStack Installation ...

Stinsen

Simple, powerful and elegant implementation of the Coordinator pattern in SwiftUI. Stinsen is written using 100% SwiftUI which makes it ...

SwiftUI Router

With SwiftUI Router you can power your SwiftUI app with path-based routing. By utilizing a path-based system, navigation in your app becomes ...

FlowStacks

This package takes SwiftUI's familiar and powerful NavigationStack API and gives it superpowers, allowing you to use the same API not just ...