Add Refresh Control

This commit is contained in:
Vlad 2020-08-26 23:40:46 +03:00
parent 0aacf470cb
commit af43eb9da7
2 changed files with 48 additions and 1 deletions

View File

@ -1,6 +1,8 @@
# TIUIElements
Bunch of useful protocols and views.
Bunch of useful protocols and views:
- RefreshControl - a basic UIRefreshControl with fixed refresh action.
# Installation via SPM

View File

@ -0,0 +1,45 @@
import UIKit
open class RefreshControl: UIRefreshControl {
private var action: Selector?
private var target: NSObjectProtocol?
public override func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControl.Event) {
guard case .valueChanged = controlEvents else {
return super.addTarget(target, action: action, for: controlEvents)
}
self.action = action
self.target = target as? NSObjectProtocol
}
public override func removeTarget(_ target: Any?, action: Selector?, for controlEvents: UIControl.Event) {
guard self.action == action, self.target?.isEqual(target) ?? false else {
super.removeTarget(target, action: action, for: controlEvents)
return
}
self.action = nil
self.target = nil
}
public override func sendActions(for controlEvents: UIControl.Event) {
guard case .valueChanged = controlEvents else {
return super.sendActions(for: controlEvents)
}
if action != nil, target != nil {
performRefreshAction()
}
}
private func performRefreshAction() {
CFRunLoopPerformBlock(CFRunLoopGetMain(), CFRunLoopMode.defaultMode.rawValue) { [weak self] in
guard let action = self?.action else {
return
}
self?.target?.perform(action)
}
}
}