A radio button with a pretty animation

Overview

LTHRadioButton

Slightly inspired by Google's material radio button.

The clip below has 3 sections: full speed, 25% and 10%, but after converting it to GIF, it actually made it longer, so the 10% part takes a really long time. Here's an mp4 link; try with Chrome if Safari doesn't work - for me it doesn't.

How to install

Swift Package Manager

NOTE: These instructions are intended for usage on Xcode 11 and higher. Xcode 11 is the first version of Xcode that integrates Swift Package Manager and makes it way easier to use than it was at the command line. If you are using older versions of Xcode, we recommend using CocoaPods.

  1. Go to File > Swift Packages > Add Package Dependency...
  2. Paste the URL to the LTHRadioButton repo on GitHub (https://github.com/rolandleth/LTHRadioButton.git) into the search bar, then hit the Next button:
  3. Select what version you want to use, then hit next (Xcode will automatically suggest the current version Up to Next Major).
  4. Select the LTHRadioButton library and then hit finish.
  5. You're done!

CocoaPods

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

gem install cocoapods

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

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

target '<Your Target Name>' do
  pod 'LTHRadioButton'
end

Then, run the following terminal command:

pod install

Manual

Drag LTHRadioButton.swift from the source folder into your Xcode project.

How to use

The initializer takes up to 3 params: a diameter, a selectedColor, and a deselectedColor. All of them are optional:

  • diameter defaults to 18
  • selectedColor defaults to a light blue
  • deselectedColor defaults to UIColor.lightGray

It doesn't use Auto Layout internally, but after initialization it will have a proper size, so you can simply create constraints based on its frame.width and frame.height.

Properties

selectedColor and deselectedColor have been made publicly customizable for cases like a tableView with alternating row and radio colors, where the tableView might dequeue a cell with one color for displaying a cell with a different color.

isSelected - Indicates whether the radio button is selected.

useTapGestureRecognizer - Indicates whether a tap gesture recognizer should be added to the control when setting callbacks. This defaults to true just so that onSelect and onDeselect can add the gesture recognizer automatically, but the recognizer is not added by default.

  • Settings this to true will also add the required UITapGestureRecognizer if needed.
  • Settings this to false will also remove the UITapGestureRecognizer if it was previously added.

Methods

init(diameter: CGFloat = 18, selectedColor: UIColor? = nil, deselectedColor: UIColor? = nil) // Colors default internally if nil.
func select(animated: Bool = true) // Selects the radio button.
func deselect(animated: Bool = true) // Deselects the radio button.

Callbacks

You can make use of the onSelect and onDeselect methods to add closures to be run when selecting/deselecting the control. Since these closures make most sense for taps and because there are no recognizers by default, these methods will also add one (and only one) UITapGestureRecognizer to the control to handle the taps; the closure calls happen right as the animations begin.

If you'd like to use the callbacks but don't need the tap gesture recognizer, you can set useTapGestureRecognizer to false.

Example

let radioButton = LTHRadioButton(selectedColor: .red)
container.addSubview(radioButton)

radioButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
  radioButton.centerYAnchor.constraint(equalTo: container.centerYAnchor),
  radioButton.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 16),
  radioButton.heightAnchor.constraint(equalToConstant: radioButton.frame.height),
  radioButton.widthAnchor.constraint(equalToConstant: radioButton.frame.width)]
)

radioButton.onSelect {
  print("I'm selected.")
}

radioButton.onDeselect {
  print("I'm deselected.")
}

[...]

radioButton.select() // I'm selected.

[...]

radioButton.deselect(animated: false) // I'm deselected.

Apps using this control

If you're using this control, I'd love hearing from you!

License

Licensed under MIT. If you'd like (or need) a license without attribution, don't hesitate to contact me.

Comments
  • Timing bug

    Timing bug

    Hi,

    I've found a weird animation bug with this control. Basically, with each view controller rotation, the animation takes a little longer to start animating.

    So for eg. after initial load - animations starts right away, no matter how many times you toggle. After first rotation takes ~0.2s to start animationg After second ~0.4s etc

    I didn't have enough core animation knowledge nor time to resolve the issue, but found that commenting all the .beginTimes "fixes it", but obviously ruins the animation timing.

    opened by alexandre-g 13
  • Not clickable by default

    Not clickable by default

    Hello,

    Essentially a radio button in web development is always clickable. However, your implementation does not provide the basic click funcion, so we need to add a tap gesture on the view and manually set to select state or not selected state based on the current state of the view. I think this initialization should be inside the view, and have a delegate that let us know when the state changed.

    Let me know what you think about it Cheers

    enhancement 
    opened by neokree 6
  • View not showing when loaded from a storyboard

    View not showing when loaded from a storyboard

    Hello,

    The view, loaded from a storyboard, isn't showing nothing. I think the problem can be the absence of the initialization method init(frame: CGRect). I have this problem with Swift 3.1 and XCode 8.3.3 I have tried to install this library with cocoapods and manually, same result.

    opened by neokree 4
  • UItapGestureRecognaizer is not working

    UItapGestureRecognaizer is not working

    func Customizepublicbtn() { radioButton = LTHRadioButton(diameter: 18, selectedColor: .red, deselectedColor: .blue) radioButton.useTapGestureRecognizer = true radioButton.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(radioButton) radioButton.rightAnchor.constraint(equalTo: imageView.rightAnchor, constant: 0).isActive = true radioButton.leftAnchor.constraint(equalTo: imageView.leftAnchor, constant: 0).isActive = true radioButton.topAnchor.constraint(equalTo: about.bottomAnchor, constant: 20).isActive = true radioButton.onSelect { print("I'm selected.") }

        radioButton.onDeselect {
            print("I'm deselected.")
        }
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
        radioButton.addGestureRecognizer(tap)
    }
    
    @objc func tapFunction(sender:UITapGestureRecognizer) {
        print("Selected")
        radioButton.select()
    }
    
    opened by yaman1987 3
  • Fix bad subview position in view initialization

    Fix bad subview position in view initialization

    Hi Roland,

    Loading the views using constraints on storyboards effect the center property, so using this property for centering subviews of a custom view is not recommended, because they will be outside your custom view. I have initialized the circle view to be positioned on the origin of the custom view, then for the other's views I used the radious variable to calculate the correct center of the view.

    opened by neokree 3
  • SPM problem

    SPM problem

    I'm using xcode 12.5.1 and i've this error: package at 'https://github.com/rolandleth/LTHRadioButton.git' @ 56ef3f3dfa5766e8b4d0c983d5e2f83a08f66daf is using Swift tools version 5.5.0 but the installed version is 5.4.0 in https://github.com/rolandleth/LTHRadioButton.git

    opened by furiosFast 2
  • Method name 'select' ERROR

    Method name 'select' ERROR

    Hi, I'm using LTHRadioButton with CocoaPods. When I was using this library, I met an error. The name of method 'select' may be not good.

    Xcode8.1 Swift3.0.1

    2016-12-06 14 42 09
    opened by masuhara 2
Owner
Roland Leth
Full-stack developer, specialized in iOS, focused on user experience.
Roland Leth
MUDownloadButton - a Progressive Download button written in pure swift and inspired by AppStore download button

MUDownloadButton is a Progressive Download button written in pure swift and inspired by AppStore download button . feel free to contribute and pull requests

Mohammad ShahibZadeh 2 Feb 20, 2022
Bar Button Item that can be moved anywhere in the screen, like Android's stickers button.

FlowBarButtonItem Bar Button Item that can be moved anywhere in the screen, like Android's stickers button. [![CI Status](http://img.shields.io/travis

noppefoxwolf 153 Sep 15, 2022
Newly is a drop in solution to add Twitter/Facebook/Linkedin style, new updates/tweets/posts available button

Newly is a drop in solution to add Twitter/Facebook/Linkedin style, new updates/tweets/posts available button. It can be used to notify user about new content availability and can other actions can be triggers using its delegate method.

Dhiraj Rajendra Jadhao 197 Sep 22, 2022
Spinner loader components with liquid animation

LiquidLoader LiquidLoader is the spinner loader UI components with liquid animation, inspired by Spinner Loader - Gooey light Effect [] (https://githu

Takuma Yoshida 1.3k Dec 21, 2022
An easy to use UI component to help display a signal bar with an added customizable fill animation

TZSignalStrengthView for iOS Introduction TZSignalStrengthView is an easy to use UI component to help display a signal bar with an added customizable

TrianglZ LLC 22 May 14, 2022
A beautiful radar view to show nearby items (users, restaurants, ...) with ripple animation, fully customizable

HGRippleRadarView Example To run the example project, clone the repo, and run pod install from the Example directory first. This project is inspired b

Hamza Ghazouani 352 Dec 4, 2022
A radio button with a pretty animation

LTHRadioButton Slightly inspired by Google's material radio button. The clip below has 3 sections: full speed, 25% and 10%, but after converting it to

Roland Leth 368 Dec 16, 2022
The official OS X client to the Radio Paradise web radio.

Introduction Radio Paradise is a unique blend of many styles and genres of music, carefully selected and mixed by two real human beings — enhanced by

Giacomo Tufano 19 Aug 17, 2022
Swift Radio is an open source radio station app with robust and professional features.

Swift Radio Swift Radio is an open source radio station app with robust and professional features. This is a fully realized Radio App built entirely i

Ahmed AlOtaibi 0 Oct 13, 2021
An iOS drop down menu with pretty animation and easy to customize.

IGLDropDownMenu An iOS drop down menu with pretty animation. Screenshot How To Use Use CocoaPods: pod 'IGLDropDownMenu' Manual Install: Just drap the

Galvin Li 1.2k Dec 27, 2022
zekunyan 608 Dec 30, 2022
RadioGroup - The missing iOS radio buttons group.

RadioGroup The missing iOS radio buttons group. Usage let radioGroup = RadioGroup(titles: ["First Option Title", "Another Option Title", "Last"]) radi

Yonat Sharon 188 Dec 20, 2022
Professional Radio Station App - now supports Swift 5 / Xcode 10!

Swift Radio Swift Radio is an open source radio station app with robust and professional features. This is a fully realized Radio App built entirely i

Matthew Fecher 2.7k Jan 7, 2023
Radio & Podcast Streaming App For WPRK

WPRK(iOS & iPadOS) Radio & Podcast Streaming App For WPRK, a licensed FCC broadcast station, The app allows users to connect to live music streams and

MwaiBanda 5 May 30, 2022
Radio Streams from all over the world. Free and Open.

RadioBrowserKit - The Swift SDK for Radio Browser Radio Streams from all over the world. Free and Open. RadioBrowserKit is a Swift package which lets

Frank Gregor 5 Oct 17, 2022
Review page interaction - handy and pretty way to ask for review.

RPInteraction Overview Review page interaction - handy and pretty way to ask for review. Inspired by dribbble shot. Requirements iOS8 Installation RPI

Nurdaulet Bolatov 27 Jul 16, 2021
Pretty GCD calls and easier code execution.

Threader Pretty GCD calls and easier code execution. Overview Threader makes GCD calls easy to read & write. It also provides a simple way to execute

Mitch Treece 35 Sep 9, 2022
A highly configurable and out-of-the-box-pretty UI library

We absolutely love beautiful interfaces! As an organization named Unicorn, we are obligated to be unique and majestic.

Clayton (McIlrath) Unicorn 225 Feb 11, 2022
Pretty iOS mobile screens + AVPlayer video view ––– made in SwiftUI

UrbanVillageProjectScreens Recreated UI screens from the conceptual Urban Village Project. Read more about the project here. Please open an issue if y

10011.co 23 Dec 29, 2022
Review page interaction - handy and pretty way to ask for review

RPInteraction Overview Review page interaction - handy and pretty way to ask for review. Inspired by dribbble shot. Requirements iOS8 Installation RPI

Nurdaulet Bolatov 27 Jul 16, 2021