LTHRadioButton - 带有漂亮动画的单选按钮
LTHRadioButton
略微受到谷歌材料单选按钮的启发。
下面的剪辑有 3 个部分:全速、25% 和 10%,但在将其转换为 GIF 后,它实际上使其更长,因此 10% 的部分需要很长时间。这是一个mp4链接;如果 Safari 不起作用,请尝试使用 Chrome - 对我来说它不起作用。
如何安装
Swift Package Manager
注意:这些说明适用于 Xcode 11 及更高版本。Xcode 11 是 Xcode 的第一个版本,它集成了 Swift Package Manager,使其比在命令行中更容易使用。如果您使用的是旧版本的 Xcode,我们建议您使用 CocoaPods。
- 转到 Swift 软件包>文件>添加软件包依赖项...
- 将 GitHub 上的存储库 URL 粘贴到 GitHub (https://github.com/rolandleth/LTHRadioButton.git) 到搜索栏中,然后点击 下一页 按钮:
LTHRadioButton
- Select what version you want to use, then hit next (Xcode will automatically suggest the current version Up to Next Major).
- Select the library and then hit finish.
LTHRadioButton
- 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 into your Xcode project using CocoaPods, specify it in your Podfile:LTHRadioButton
Then, run the following terminal command:
pod install
Manual
Drag from the folder into your Xcode project.LTHRadioButton.swift
source
How to use
The initializer takes up to 3 params: a , a , and a . All of them are optional:diameter
selectedColor
deselectedColor
diameter
defaults to18
selectedColor
defaults to a light bluedeselectedColor
defaults toUIColor.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 and .frame.width
frame.height
Properties
selectedColor
and have been made publicly customizable for cases like a with alternating row and radio colors, where the might dequeue a cell with one color for displaying a cell with a different color.deselectedColor
tableView
tableView
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 just so that and can add the gesture recognizer automatically, but the recognizer is not added by default.true
onSelect
onDeselect
- Settings this to will also add the required if needed.
true
UITapGestureRecognizer
- Settings this to will also remove the if it was previously added.
false
UITapGestureRecognizer
Methods
Callbacks
You can make use of the and 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) to the control to handle the taps; the closure calls happen right as the animations begin.onSelect
onDeselect
UITapGestureRecognizer
If you'd like to use the callbacks but don't need the tap gesture recognizer, you can set to .useTapGestureRecognizer
false
Example
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.