JPSwitchButton 0.2.0

JPSwitchButton 0.2.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Aug 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by julian Panucci.



  • By
  • julp04

JPSwitchButton

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

JPSwitchButton is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "JPSwitchButton"

Usage

Initalizing Button

Button Without an Image

let switchButton = JPSwitchButton(frame: buttonFrame, offColor: .white, onColor: blue, image: nil, title: "Press me to turn on", description: "Currently I am off", isOn: false)

Alt text

Button With an Image

let twitterButton = JPSwitchButton(frame: buttonFrame2, offColor: .white, onColor: twitterBlue, image: #imageLiteral(resourceName: "twitter_on"), title: "Connect with Twitter", description: "Add your twitter account!")

Alt text

On Button Click

You can choose what happens when your button is clicked by writing your code inside the block

switchButton.onClick =  {

          switchButton.switchState()
          switchButton.buttonTitle = switchButton.isOn ? "Press me to turn off" : "Press me to turn on"
          switchButton.buttonDescription = switchButton.isOn ? "Currently I am on" : "Currently I am off"
}

On long press

Create different action for a long press

twitterButton.onLongPress =  {
        if twitterButton.isOn {
            let url = URL(string: "http://twitter.com/\(username)")!
            UIApplication.shared.openURL(url)
        }
    }

Alt text

Changing Button State

Switch State

Will turn it off if on, and vice versa

switchButton.onClick =  {
         switchButton.switchState()
}

Turn On

switchButton.onClick =  {
         switchButton.turnOn()
}

Turn Off

switchButton.onClick =  {
         switchButton.turnOff()
}

Author

julp04, [email protected]

License

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