Skip to content

Instantly share code, notes, and snippets.

View PimCoumans's full-sized avatar
thinking about making coffee

Pim PimCoumans

thinking about making coffee
View GitHub Profile
@PimCoumans
PimCoumans / UIView+TimingFunction.swift
Last active April 22, 2022 09:51
Adds CAMediaTimingFunction to UIView animation using a convenient CATransaction wrapper method
extension CATransaction {
/// Executes the provided `actions` closure wrapped in `CATransaction`.
/// Optionally adds all the specific properties to commit the transaction with
/// - Parameters:
/// - duration: Duration of transaction
/// - timingFunction: Specific timing function to use with transaction
/// - disableActions: Wether actual animation should happen during transaction
/// - actions: What to do while transaction is commited
/// - completion: Closure to be executed when transaction is completes
///
import Foundation
extension CFRunLoop {
static func performNext(work: @escaping () -> Void) {
CFRunLoopPerformBlock(CFRunLoopGetMain(), CFRunLoopMode.defaultMode.rawValue, work)
}
}
@PimCoumans
PimCoumans / Configurable.swift
Last active November 19, 2020 10:50
Allows classes to be configured and initialized with a closure
import UIKit
/// Adds a `configure` method with a closure that is executed with `Self` as a parameter
protocol Configurable { }
extension Configurable {
/// Configures the instance with a closure that is executed with `Self` as a parameter
/// - Parameter configurer: Closure exectured immediately with `Self` as a parameter
/// - Parameter instance: Use this parameter to 'configure' the instance
func configure(with configurer: (_ instance: Self) -> Void) {
//
// TimelineLayout.swift
// OKVideo
//
// Created by Pim Coumans on 12/11/2020.
// Copyright © 2020 pixelrock. All rights reserved.
//
import UIKit
@PimCoumans
PimCoumans / CustomButton.swift
Last active July 8, 2022 09:16
UIButton subclass with per-state custom values like background and image colors, extendable with whatever value you want to update
class Button: UIButton {
private class Subview: UIView {
// Never allow isUserInteractionEnabled in any button subview
override var isUserInteractionEnabled: Bool {
get { false }
set { super.isUserInteractionEnabled = false }
}
}
@PimCoumans
PimCoumans / CornerRounding.swift
Created June 5, 2020 13:58
Simple UIView rounded corners with iOS 13’s continuous corners
import UIKit
extension CACornerMask {
static let all: CACornerMask = [
.layerMaxXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner
]
}
extension UIView {
func roundCorners(by radius: CGFloat, corners: CACornerMask = .all) {
@PimCoumans
PimCoumans / LayerView.swift
Created June 5, 2020 13:16
UIView with a generic CALayer class
class LayerView<Layer: CALayer>: UIView {
override static var layerClass: AnyClass {
return Layer.self
}
var castedLayer: Layer {
return super.layer as! Layer
}
}
@PimCoumans
PimCoumans / QuickConnect.swift
Last active October 4, 2022 05:33
Finding your external Synology URL though the undocumented QuickConnect API
import Foundation
// Based on example by Thomas Theunen: https://thomastheunen.eu/2017/07/04/diving-into-synology-quickconnect-and-creating-a-javascript-library/
struct QuickConnect {
private static let endpointURL = URL(string: "https://global.quickconnect.to/Serv.php")!
enum Error: Swift.Error {
case noAddressFound
@PimCoumans
PimCoumans / resize.swift
Last active May 7, 2022 18:20
Video aspect transform resizing
// let targetSize = CGSize(width: 100, height: 100) // whatever you want
var transform = videoAssetTrack.preferredTransform
let naturalSize = videoAssetTrack.naturalSize
let videoSize = CGRect(origin: .zero, size: naturalSize).applying(videoAssetTrack.preferredTransform).size
let rotation = atan2(transform.b, transform.a)
let isRotated = rotation.remainder(dividingBy: .pi) > 0.01
let offset = CGPoint.zero.applying(transform.inverted())

Keybase proof

I hereby claim:

  • I am pimcoumans on github.
  • I am pimcoumans (https://keybase.io/pimcoumans) on keybase.
  • I have a public key whose fingerprint is B7D5 832A C1B7 A316 1B44 9C2D A614 9B6B D8D2 9B7C

To claim this, I am signing this object: