LTHRadioButton - A radio button with a pretty animation

Overview

LTHRadioButton

Build status

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

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 '' 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
Customizable download button with progress and transition animations. It is based on Apple's App Store download button.

AHDownloadButton is a customizable download button similar to the download button in the latest version of Apple's App Store app (since iOS 11). It fe

Amer Hukić 465 Dec 24, 2022
Revamped Download Button. It's kinda a reverse engineering of Netflix's app download button.

NFDownloadButton Revamped Download Button Requirements Installation Usage License Requirements iOS 8.0+ Swift 4.2+ Xcode 10.0+ Installation CocoaPods

Leonardo Cardoso 433 Nov 15, 2022
IGStoryButtonKit provides an easy-to-use button with rich animation and multiple way inspired by instagram story/stories.

Introduction Have you ever seen UI like instagram story, haven't you? Actually, features like instagram story have been implemented in many applicatio

mutation 34 Nov 8, 2022
Cute Animated Button written in Swift.

DOFavoriteButton Cute Animated Button written in Swift. It could be just right for favorite buttons! Requirements iOS 7.0+ Swift 1.2 Installation Cart

Daiki Okumura 3.6k Dec 29, 2022
Customizable and easy to use expandable button in Swift.

ExpandableButton Requirements iOS 9.0+ Installation CocoaPods: Add the following line to your Podfile: pod 'ExpandableButton' #for swift less than 4.

Dmytro Mishchenko 98 Dec 5, 2022
Easily customizable floating button menu created with SwiftUI

FloatingButton Easily customizable floating button menu created with SwiftUI We are a development agency building phenomenal apps. Usage Create main b

Exyte 715 Dec 30, 2022
Floaty is simple floating action button for iOS.

Floaty is simple floating action button for iOS. (formerly KCFloatingActionButton) Why change the name? Follow the swift naming convention. KCF

Lee Sun-Hyoup 1.5k Jan 7, 2023
Multiple state tap-to-toggle UIButton (like old camera flash button)

Multiple State Toggle UIButton A UIButton subclass that implements tap-to-toggle button text. (Like the camera flash and timer buttons) Usage Just cre

Yonat Sharon 83 Oct 11, 2022
Lickable-Button We made the buttons on the screen look so good you'll want to lick them

Lickable-Button We made the buttons on the screen look so good you'll want to lick them. - Steve Jobs A little SwiftUI button project at WWDC 2021 Lic

Nate Thompson 14 Dec 29, 2021
Craft that perfect SwiftUI button effect 👌🏼

buttoncraft (SwiftUI 3.0 App) Experimenting with SwiftUI 3.0 whilst creating a practical app to craft that perfect button style. ✈️ Testflight https:/

An Trinh 188 Dec 28, 2022
iOS Pod for a Soft UI (Neumorphic) Button for UIKit written in Swift

iOS Pod for a Soft UI (Neumorphic) Button for UIKit written in Swift

Pallav Agarwal 21 Oct 23, 2022
Custom loading button with progress swiftui

CustomLoadingButton Simple Custom Loading Progress Button for SwiftUI Version 1.0.0 This version requires Xcode 11+ SwiftUI iOS 13+ macOS 10.15+ Insta

Tariqul 1 Dec 14, 2022
An open-source library to use with SwiftUI, It allows you to create Floating menu action button.

Floating Menu Action Button Example Overview This is an open-source library to use with SwiftUI. It allows you to create Floating menu action button. Ins

ugo 3 Aug 19, 2022
iOS 7-style bouncy button.

SSBouncyButton SSBouncyButton is simple button UI component with iOS 7-style bouncy animation. Take A Look Try It! pod 'SSBouncyButton', '~> 1.0' Use

StyleShare 310 Dec 15, 2022
Flat button with 9 different states using POP

VBFPopFlatButton Flat button with 21 different states and 2 types animated using pop. These are some examples of both types in different states: And h

Victor Baro 3.1k Nov 30, 2022
Flat design pressable button for iOS developers.

HTPressableButton HTPressableButton is designed for iOS developers to be able to spend time developing ideas, not building basic buttons. These stylis

Famolus 859 Dec 12, 2022
Material Design Floating Action Button in liquid state

LiquidFloatingActionButton [] (https://github.com/Carthage/Carthage) LiquidFloatingActionButton is floating action button component of material design

Takuma Yoshida 3.8k Dec 29, 2022
Simple and customizable button in Swift

SwiftyButton Maintainer(s): @nickm01 @pmacro @aryamansharda Simple and customizable button in Swift. Installation Cocoapods pod 'SwiftyButton' Cartha

Scoop 542 Dec 13, 2022
Animated Play and Pause Button written in Swift, using CALayer, CAKeyframeAnimation.

AnimatablePlayButton Animated Play and Pause Button written in Swift, using CALayer, CAKeyframeAnimation. features Only using CAShapeLayer, CAKeyframe

keishi suzuki 77 Jun 10, 2021