Skip to content

Instantly share code, notes, and snippets.

@Bogidon
Bogidon / AnimatedGrowingTextView.swift
Last active January 11, 2023 07:04
A short and understandable UITextView subclass that grows with its text. Animatable. Updates intrinsicContentSize. If you don't need animations check out https://gist.github.com/Bogidon/cc0c9ae6f041413c39fb0ff146ad1b18
//
// AnimatedGrowingTextView.swift
// https://gist.github.com/Bogidon/632e265b784ef978d5d8c0b86858c2ee
//
// Created by Bogdan Vitoc on 02/15/2017.
// Distributed under the MIT License: https://gist.github.com/Bogidon/632e265b784ef978d5d8c0b86858c2ee#file-license
//
import UIKit
@crisbit
crisbit / ElapsedTime.swift
Created December 14, 2016 13:10
Calculate elapsed time in Swift
let start = Date()
print("Elapsed time: \(start.timeIntervalSinceNow) seconds")
@kandelvijaya
kandelvijaya / PrecisionTimer.swift
Created October 24, 2016 20:41
Precision Timing in iOS/OSX/Swift
//: Playground - noun: a place where code can play
import UIKit
//Most precise time keeper
// for more information on the benchmarks go to www.kandelvijaya.com
func timeBlockWithMach(_ block: () -> Void) -> TimeInterval {
var info = mach_timebase_info()
guard mach_timebase_info(&info) == KERN_SUCCESS else { return -1 }
@cuxaro
cuxaro / resizeImage.swift
Created October 19, 2016 15:01
Resize Image with Swift 3
func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0,width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
@radutzan
radutzan / UIButton+Actions.swift
Last active December 13, 2018 19:43
A way to declare a UIButton's tap, touch down, and "touch lift" actions through closure properties. See comments for more info.
typealias ButtonAction = (button: UIButton) -> Void
class ButtonActionWrapper: NSObject {
var action: ButtonAction
init(action: ButtonAction) {
self.action = action
}
}
@brianleroux
brianleroux / wtf-sns-apns.js
Created August 5, 2016 23:00
Send a silent push notification to APNS with AWS SNS.
sns.publish({
TargetArn: device.arn,
MessageStructure: 'json',
Message: JSON.stringify({
default: 'you will never see this muah!',
APNS_SANDBOX: JSON.stringify({
aps: {
'alert': '',
'content-available': 1
},
@onmyway133
onmyway133 / Init in protocol extension.swift
Last active June 7, 2019 10:01
Init in protocol extension.swift
protocol P {
init()
}
extension P {
init(something: AnyObject) {
self.init()
print("P")
}
}
@lzell
lzell / string_to_data_back.swift
Last active September 23, 2019 19:28
Swift string to data (bytes) and back
// (string, swift, bytes, data, buffer, cstring)
print("--- using nulTerminated ---")
let x : String = "hello"
let buf = x.nulTerminatedUTF8
print(buf)
print("\n--- using [UInt8] ---")
let buf2 : [UInt8] = [UInt8](x.utf8)
print(buf2)
@eddieespinal
eddieespinal / cropCameraImage.swift
Last active April 14, 2021 08:16
Crops a Picture from AVCaptureSession to the bounds of the AVCaptureVideoPreviewLayer (so Preview = CameraImage) - Swift Version
//This is the swift version of the following gist by @shexbeer https://gist.github.com/shexbeer/cb069d36ca8ec5edb515
func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {
var image = UIImage()
let previewImageLayerBounds = previewLayer.bounds
let originalWidth = original.size.width
let originalHeight = original.size.height
@nazywamsiepawel
nazywamsiepawel / pause_resume.swift
Created January 4, 2016 22:01
Pause / Resume CABasicAnimation with Swift
func pauseAnimation(){
var pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
func resumeAnimation(){
var pausedTime = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0