Skip to content

Instantly share code, notes, and snippets.

@drosenstark
drosenstark / Donut.swift
Last active April 6, 2019 11:59
(thanks to Paintcode) Draw a hollow circle in swift, or just a piece of one... angle starts at bottom and rotates around clockwise.
class func drawHollowCircle(circleColor: UIColor, rect: CGRect, startAngle: CGFloat = 1, endAngle: CGFloat = 360, outerRingProportion: CGFloat = 1, innerRingProportion: CGFloat = 0.5, drawShadow : Bool) {
//// Variable Declarations
let startAngleCalced: CGFloat = -startAngle + 270
let endAngleCalced: CGFloat = -endAngle + 270
let innerRingDiameter: CGFloat = rect.size.width * innerRingProportion
let innerRingOffset: CGFloat = 0.5 * (rect.size.width - innerRingDiameter)
let innerRingRect = CGRectMake(innerRingOffset, innerRingOffset, innerRingDiameter, innerRingDiameter)
let outerRingDiameter: CGFloat = rect.size.width * outerRingProportion
let outerRingOffset: CGFloat = 0.5 * (rect.size.width - outerRingDiameter)
@MaximAlien
MaximAlien / ViewControllerPresentation
Created December 21, 2015 16:00
[iOS] Change corner radius and size of the UIViewController presented modally
// For UIViewController inside UINavigationController
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.navigationController.view.superview.bounds = CGRectMake(0, 0, 300, 300);
self.navigationController.view.superview.layer.cornerRadius = 2.0f;
}
// Usual presentation
@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
@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
@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)
@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")
}
}
@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
},
@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
}
}
@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()
@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 }