Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Fil Sviatoslav filsv

🏠
Working from home
View GitHub Profile
@filsv
filsv / WatchSessionManager.swift
Last active Apr 29, 2021
WatchConnectivity Singleton (Swift 5+, iOS+WatchOS Targets) + How to (comment below)
View WatchSessionManager.swift
import WatchConnectivity
class WatchSessionManager: NSObject, WCSessionDelegate {
static let sharedManager = WatchSessionManager()
private override init() {
super.init()
}
private let session: WCSession? = WCSession.isSupported() ? WCSession.default : nil
@filsv
filsv / VideoThumbnailHelper.swift
Last active Sep 20, 2019
Image thumbnail from video
View VideoThumbnailHelper.swift
import AVFoundation
func getThumbnail(url: URL) -> UIImage? {
do {
let asset = AVURLAsset(url: url , options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
@filsv
filsv / DegreesRadiansHelper.swift
Last active May 28, 2018
Degrees <=> Radians
View DegreesRadiansHelper.swift
class DegreesRadiansHelper {
static func deg2Rad(_ degrees: Double) -> Double {
return degrees * .pi / 180
}
static func rad2Deg(_ radians: Double) -> Double {
return radians * 180.0 / .pi
}
}
@filsv
filsv / DelayHelper.swift
Created May 28, 2018
Cancelable Delay Function - Swift 4.0
View DelayHelper.swift
class DelayHelper {
private var cancelled = false
static func run(delay: Double, closure: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
if !self.cancelled {
closure()
}
}
}
@filsv
filsv / UIViewExtension+RoundCorners.swift
Last active Mar 16, 2017
Apply round corner/s on UIView, you can apply round corner to any side of UIView
View UIViewExtension+RoundCorners.swift
func roundCorners(corners: UIRectCorner, radius: CGFloat)
{
let size = CGSize(width: radius, height: radius)
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: size)
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
@filsv
filsv / CALayer+Border.swift
Created Dec 26, 2016
Swift 3 Extension for Adding Border on one of the sides (or for all sides).
View CALayer+Border.swift
// Layer extension for adding border on one of the sides
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let border = CALayer()
switch edge {
case UIRectEdge.top:
border.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: thickness)
@filsv
filsv / CellReordering - UITableView.swift
Last active Dec 26, 2016
UITableViewCell Reordering
View CellReordering - UITableView.swift
var arrays: [[String: AnyObject]] = [[String: AnyObject]]()
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let dic = self.arrays[sourceIndexPath.section]
if let array = dic["arrays"] as? NSMutableArray