Если вы хотите создать RadioButton без использования каких-либо внешних модулей, вы можете использовать этот код:
import Foundation
import UIKit
class HRadioButton: UIButton {
@IBOutlet var otherButtons: [HRadioButton]!
var hSelected = false {
didSet {
if hSelected {
if imageView != nil{
self.setImage(checkedImage, for: .normal)
}else{
self.setImage(checkedImage, for: .normal)
print("image null")
}
if self.otherButtons != nil {
for button in self.otherButtons {
button.hSelected = false
button.imageView?.image = self.unCheckedImage
}
} else {
print("Button is null ")
}
}
}
}
var checkedImage: UIImage!, unCheckedImage: UIImage!
override func awakeFromNib() {
super.awakeFromNib()
checkedImage = #imageLiteral(resourceName: "ic_radio_button_checked").maskWithColor(color: tintColor)
unCheckedImage = #imageLiteral(resourceName: "ic_radio_button_unchecked").maskWithColor(color: tintColor)
setImage(unCheckedImage, for: .normal)
alignImageRight()
self.onTap {
self.imageView?.image = self.checkedImage
self.hSelected = true
if self.otherButtons != nil {
for button in self.otherButtons {
button.hSelected = false
button.imageView?.image = self.unCheckedImage
}
}
}
}
}
extension UIImage {
func maskWithColor(color: UIColor) -> UIImage? {
let maskImage = cgImage!
let width = size.width
let height = size.height
let bounds = CGRect(x: 0, y: 0, width: width, height: height)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!
context.clip(to: bounds, mask: maskImage)
context.setFillColor(color.cgColor)
context.fill(bounds)
if let cgImage = context.makeImage() {
let coloredImage = UIImage(cgImage: cgImage)
return coloredImage
} else {
return nil
}
}
}
extension UIView {
// In order to create computed properties for extensions, we need a key to
// store and access the stored property
fileprivate struct AssociatedObjectKeys {
static var tapGestureRecognizer = "MediaViewerAssociatedObjectKey_mediaViewer"
}
fileprivate typealias Action = (() -> Void)?
// Set our computed property type to a closure
fileprivate var tapGestureRecognizerAction: Action? {
set {
if let newValue = newValue {
// Computed properties get stored as associated objects
objc_setAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
get {
let tapGestureRecognizerActionInstance = objc_getAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer) as? Action
return tapGestureRecognizerActionInstance
}
}
// This is the meat of the sauce, here we create the tap gesture recognizer and
// store the closure the user passed to us in the associated object we declared above
public func onTap(action: (() -> Void)?) {
self.isUserInteractionEnabled = true
self.tapGestureRecognizerAction = action
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
self.addGestureRecognizer(tapGestureRecognizer)
}
// Every time the user taps on the UIImageView, this function gets called,
// which triggers the closure we stored
@objc fileprivate func handleTapGesture(sender: UITapGestureRecognizer) {
if let action = self.tapGestureRecognizerAction {
action?()
} else {
print("no action")
}
}
}
из xcode storyboard ui назначьте класс HRadioButton в качестве класса Button, тогда вы найдете другиеButtons вИнспектор подключений изображение значка Инспектора подключений в Xcode
в xcode вы найдете другие кнопки Появитесь там, поэтому перетащите его на другие кнопки
и повторите этот процесс на всех кнопкахВы хотите сделать его как Radio Group, например, у меня есть кнопка A, кнопка B, кнопка C. Я назначу класс кнопке A, затем перетащу другие кнопки из Инспектора подключений на кнопки B и C, затем назначу кнопку Class To B и заставлю другие кнопки ссылатьсяк кнопкам A, C и т. д.
Позже вы можете получить выбранный Btn из свойства hSelected