定时器控件,上传图片指定类型

This commit is contained in:
DDIsFriend
2024-04-02 10:26:37 +08:00
parent 8ead78477a
commit 4d20a989ff
46 changed files with 8461 additions and 9944 deletions

View File

@@ -0,0 +1,189 @@
//
// GCDTimer.swift
// DDTimer
// Created by DDIsFriend on 2023/9/15.
import Foundation
import DDLog
public let GCDT = GCDTimer.default
open class GCDTimer {
public static let `default` = GCDTimer()
private var timerSources : [GCDTimerAbstract] = []
private var timerAddOrRemoveQueue = DispatchQueue(label: "GCDTimerAddOrRemoveQueue") // timerSourcesGCDTimerAbstract
///
/// - Parameters:
/// - named:
/// - delay:
/// - repeating:
/// - repeatCount:
/// - repeatHandler:
/// - cancelHandler: cancelHandlercompletionHandler
/// - completionHandler: completionHandlercancelHandler
public func addGCDTimer(named timerName:String,delay:DispatchTimeInterval,repeating:DispatchTimeInterval,repeatCount:Int = Int.max,repeatHandler:@escaping (() -> Void),cancelHandler:(() -> Void)? = nil,completionHandler:(() -> Void)? = nil) {
timerAddOrRemoveQueue.async { [weak self] in
if let _ = self?.timerSources.first(where: { gcdTimerA in
gcdTimerA.timerName == timerName
}) {
DDLog(message: "定时器\'\(timerName)\'重复添加,添加失败")
return
}
let gcdTimerA = GCDTimerAbstract(named: timerName)
gcdTimerA.addGCDTimer(delay: delay, repeating: repeating, repeatCount: repeatCount) {
repeatHandler()
} cancelHandler: {[weak self] (timerName,canceledCompletionHandler) in
//
self?.removeGCDTimer(named: timerName,removeComletionHandler: {
//
if let cancelHandler {
cancelHandler()
}
//
if let canceledCompletionHandler {
canceledCompletionHandler()
}
})
} completionHandler: {[weak self] (timerName,canceledCompletionHandler) in
//
self?.removeGCDTimer(named: timerName,removeComletionHandler: {
//
if let completionHandler {
completionHandler()
}
//
if let canceledCompletionHandler {
canceledCompletionHandler()
}
})
}
self?.timerSources.append(gcdTimerA)
DDLog(message: "定时器\'\(timerName)\'添加成功")
//
self?.logTimerName()
}
}
func removeGCDTimer(named timerName:String,removeComletionHandler:@escaping () -> Void) {
timerAddOrRemoveQueue.async { [weak self] in
//
if let index = self?.timerSources.firstIndex(where: { gcdTimerA in
gcdTimerA.timerName == timerName
}) {
self?.timerSources.remove(at: index)
DDLog(message: "定时器\'\(timerName)\'移除成功")
//
DispatchQueue.global().async {
removeComletionHandler()
}
}
//
self?.logTimerName()
}
}
/// canceledCompletionHandlercanceledCompletionHandler
/// - Parameters:
/// - timerName:
/// - canceledCompletionHandler:
public func cancelGCDTimer(named timerName:String,canceledCompletionHandler:(() -> Void)? = nil) {
timerAddOrRemoveQueue.async {[weak self] in
// timerSources
if let gcdTimerA = self?.timerSources.first(where: { gcdTimerA in
gcdTimerA.timerName == timerName
}) {
gcdTimerA.canceledCompletionHandler = canceledCompletionHandler
gcdTimerA.cancelGCDTimer(isCanceledManually: true)
}else{
//
DispatchQueue.global().async {
if let canceledCompletionHandler {
canceledCompletionHandler()
}
}
//
self?.logTimerName()
}
}
}
public func containsGCDTimer(named timerName:String) -> Bool {
if let _ = timerSources.first(where: { gcdTimerA in
gcdTimerA.timerName == timerName
}) {
return true
}
return false
}
func logTimerName() {
var array : [String] = []
for gcdTimerA in timerSources {
array.append(gcdTimerA.timerName)
}
DDLog(message:("剩余定时器:\'\(array)\'"))
array.removeAll()
}
}
class GCDTimerAbstract {
private let timerSource = DispatchSource.makeTimerSource(flags: .strict,queue: DispatchQueue(label: "GCDTimerAbstract.makeTimerSource",attributes: [.concurrent]))
public var timerName : String
public var isCanceledManually : Bool = false //
public var canceledCompletionHandler:(() -> Void)? //
init(named timerName: String) {
self.timerName = timerName
}
public func addGCDTimer(delay:DispatchTimeInterval,repeating:DispatchTimeInterval,repeatCount:Int,repeatHandler:@escaping (() -> Void),cancelHandler:((String,(() -> Void)?) -> Void)? = nil,completionHandler: ((String,(() -> Void)?) -> Void)? = nil) {
//
var leftRepeatCount = repeatCount
timerSource.setEventHandler(handler: {[weak self] in
//
repeatHandler()
leftRepeatCount-=1
//
if (leftRepeatCount > 0) == false {
// cancelcanceledCompletionHandler
self?.cancelGCDTimer(isCanceledManually: false)
}
})
timerSource.setCancelHandler(handler: {[weak self] in
if self?.isCanceledManually == true {
// ,
if let cancelHandler,let timerName = self?.timerName {
cancelHandler(timerName,self?.canceledCompletionHandler)
}
}else{
//
if let completionHandler,let timerName = self?.timerName {
completionHandler(timerName,self?.canceledCompletionHandler)
}
}
})
timerSource.schedule(wallDeadline: DispatchWallTime.now() + delay,repeating: repeating)
timerSource.activate()
}
// cancelHander
public func cancelGCDTimer(isCanceledManually:Bool) {
self.isCanceledManually = isCanceledManually
timerSource.cancel()
}
}

19
Pods/DDTimer/LICENSE generated Normal file
View File

@@ -0,0 +1,19 @@
Copyright (c) 2023 DDIsFriend <DDIsFriend@163.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

29
Pods/DDTimer/README.md generated Normal file
View File

@@ -0,0 +1,29 @@
# DDTimer
[![CI Status](https://img.shields.io/travis/DDIsFriend/DDTimer.svg?style=flat)](https://travis-ci.org/DDIsFriend/DDTimer)
[![Version](https://img.shields.io/cocoapods/v/DDTimer.svg?style=flat)](https://cocoapods.org/pods/DDTimer)
[![License](https://img.shields.io/cocoapods/l/DDTimer.svg?style=flat)](https://cocoapods.org/pods/DDTimer)
[![Platform](https://img.shields.io/cocoapods/p/DDTimer.svg?style=flat)](https://cocoapods.org/pods/DDTimer)
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Requirements
## Installation
DDTimer is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'DDTimer'
```
## Author
DDIsFriend, DDIsFriend@163.com
## License
DDTimer is available under the MIT license. See the LICENSE file for more info.