This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIApplication { | |
#if os(iOS) | |
@available(iOS 13.0, *) | |
static var customKeyWindow: UIWindow? { | |
if #available(iOS 15.0, *) { | |
let window = UIApplication | |
.shared | |
.connectedScenes | |
.compactMap {($0 as? UIWindowScene)?.keyWindow } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIViewController { | |
func topVisibleViewController() -> UIViewController { | |
/// A ---- present ---- B ---- present ---- C | |
/// A is presentingViewController (B.presentingViewController == A) | |
/// anchor point B < | |
/// C is presentedViewController (B.presentedViewController == C) | |
if self.presentedViewController != nil { | |
return self.presentedViewController!.topVisibleViewController() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
final class EventSubscription<S: Subscriber, C: UIControl>: Subscription where S.Input == C, S.Failure == Never { | |
private var subscriber: S? | |
private let control: C | |
init(subscriber: S, control: C, event: UIControl.Event) { | |
self.subscriber = subscriber |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Binding { | |
func onChange(_ handler: @escaping (Value) -> Void) -> Binding<Value> { | |
Binding { | |
self.wrappedValue | |
} set: { newValue in | |
self.wrappedValue = newValue | |
handler(newValue) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// findUniqueNum | |
/// eg: [1, 2, 4, 9, 2, 4, 1] ,find number 9 | |
/// - Parameter arr: [Int] | |
/// - Returns: resut | |
func findUniqueNum(in arr: [Int]) -> Int { | |
return arr.reduce(0) { $0 ^ $1 } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 测量一段代码的执行时间 | |
/// - Parameters: | |
/// - identifier: 标示执行代码的identifier | |
/// - operation: 执行的代码 | |
func measureExecuteTime(identifier: String, operation: () -> Void) { | |
let startTime = CFAbsoluteTimeGetCurrent() | |
operation() | |
let endTime = CFAbsoluteTimeGetCurrent() | |
let duration = endTime - startTime | |
print("\(identifier) execute for \(duration)s.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 以指定的宽度分割数组 | |
/// - Parameters: | |
/// - arr: 任意数组 | |
/// - each: 每几个一组 | |
/// - Returns: 返回分组后二维数组 | |
func splitArray<T>(arr: [T], by each: Int) -> [[T]] { | |
var resultArray = [[T]]() | |
for i in stride(from: 0, to: arr.count, by: each) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Date { | |
/// 时间戳转为格式化时间 | |
/// | |
/// - Parameter stamp: 时间戳 | |
/// - Returns: 格式化时间 | |
static func toMessageString(stamp: TimeInterval) -> String { | |
let date = Date(timeIntervalSince1970: stamp) | |
return toMessageString(date) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIDevice+XMUtils.h | |
// AwesomeTips | |
// | |
// Created by kangzubin on 2018/9/20. | |
// Copyright © 2018 KANGZUBIN. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 为UIView的内容生成一张图片 | |
/// | |
/// - Parameter view: 生成图片的View | |
/// - Returns: 返回生成的图片 | |
func generatePictureIn(view: UIView) -> UIImage? { | |
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0) | |
guard let ctx = UIGraphicsGetCurrentContext() else { return nil } | |
view.layer.render(in: ctx) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() |
NewerOlder