Skip to content

Interactive button that can toggle into a fluid and animated looking on and off state

License

Notifications You must be signed in to change notification settings

julpanucci/JPSwitchButton

Repository files navigation

JPSwitchButton

CI Status Version License Platform

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, julpanucci@gmail.com

License

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

About

Interactive button that can toggle into a fluid and animated looking on and off state

Resources

License

Stars

Watchers

Forks

Packages

No packages published