53 lines
1.2 KiB
Swift
53 lines
1.2 KiB
Swift
//
|
|
// Clickable.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 3/29/23.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
public protocol Clickable where Self: UIControl {}
|
|
|
|
extension Clickable where Self: Handlerable {
|
|
public func addEvent(event: UIControl.Event, block: @escaping (Self)->()) {
|
|
publisher(for: event)
|
|
.sink(receiveValue: { c in
|
|
block(c)
|
|
}).store(in: &subscribers)
|
|
}
|
|
}
|
|
|
|
extension Clickable where Self: ButtonBase {
|
|
public var onClick: ((Self) -> ())? {
|
|
get { return nil }
|
|
set {
|
|
if let newValue {
|
|
onClickSubscriber = publisher(for: .touchUpInside)
|
|
.sink { c in
|
|
newValue(c)
|
|
}
|
|
} else {
|
|
onClickSubscriber = nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Clickable where Self: Control {
|
|
public var onClick: ((Self) -> ())? {
|
|
get { return nil }
|
|
set {
|
|
if let newValue {
|
|
onClickSubscriber = publisher(for: .touchUpInside)
|
|
.sink { c in
|
|
newValue(c)
|
|
}
|
|
} else {
|
|
onClickSubscriber = nil
|
|
}
|
|
}
|
|
}
|
|
}
|