Skip to content

dimitris-c/ToggleSwitch

Repository files navigation

ToggleSwitch

Platforms License

Swift Package Manager Carthage compatible CocoaPods compatible

BuddyBuild JetpackSwift

A simple and custom UISwitch made out of images.

Requirements

  • iOS 11.0+
  • Xcode 9.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.1.0+ is required to build ToggleSwitch 1.0+.

To integrate ToggleSwitch into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'ToggleSwitch', '~> 1.0'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate ToggleSwitch into your Xcode project using Carthage, specify it in your Cartfile:

github "ToggleSwitch/ToggleSwitch" ~> 1.0

Swift Package Manager

To use ToggleSwitch as a Swift Package Manager package just add the following in your Package.swift file.

import PackageDescription

let package = Package(
    name: "HelloToggleSwitch",
    dependencies: [
        .Package(url: "https://github.com/dimitris-c/ToggleSwitch.git", "1.0")
    ]
)

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate ToggleSwitch into your project manually.

Embeded Binaries

  • Download the latest release from https://github.com/dimitris-c/ToggleSwitch/releases
  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
  • In the tab bar at the top of that window, open the "General" panel.
  • Click on the + button under the "Embedded Binaries" section.
  • Add the downloaded ToggleSwitch.framework.
  • And that's it!

Usage

Pretty standard integration as it's like adding a UISwitch.

let images = ToggleSwitchImages(baseOnImage: UIImage(named: "base_on"), 
                                baseOffImage: UIImage(named: "base_off"),
                                thumbOnImage: UIImage(named: "thumb_on"),
                                thumbOffImage: UIImage(named: "thumb_off"))

// ToggleSwitch will use the baseOnImage to construct the size of the control
let onOffSwitch = ToggleSwitch(with: images)
onOffSwitch.frame.origin = CGPoint(x: 100, y: 100)
self.addSubview(onOffSwitch)                  

The control exposes two ways of retrieving when the value/state has changed.

Using Block

onOffSwitch.stateChange = { isOn in 
    if isOn {
        // do something
    }
}

Using Target-Action

onOffSwitch.addTarget(self, action: #selector(toggleValueChanged), for: .valueChanged)

@objc func toggleValueChanged(control: ToggleSwitch) {
    if onOffSwitch.isOn { 
        // do something
    }
}

Similar to UISwitch, ToggleSwitch exposes isOn and setOn(on:animated:) method

onOffSwitch.isOn = true

onOffSwitch.setOn(on: false, animated: true)

License

ToggleSwitch is released under the MIT license. See LICENSE for details.