Skip to content

Instantly share code, notes, and snippets.

View KyleGoslan's full-sized avatar

Kyle Goslan KyleGoslan

View GitHub Profile
@KyleGoslan
KyleGoslan / AnimateMapCamera.swift
Last active March 21, 2016 00:06
Animate MKMapCamera in Swift 2
//Create a new MKMapCamera object.
//Looking at, is where you want it to look at, in this example I keep it the same so use the map views current center coordinate
//I up the altitude (zoom out) by adding 1600 on to the current position
//Pitch and heading are the rotation and angle of the camera
let newCameraPosition = MKMapCamera(lookingAtCenterCoordinate: mapView.centerCoordinate, fromDistance: mapView.camera.altitude + 1600, pitch: 50, heading: -30)
//Animate the camera, here over 5 seconds with a 1.5 second delay, the options are fairly self explanitory.
UIView.animateWithDuration(5, delay: 1.5, options: [.AllowUserInteraction, .CurveEaseInOut], animations: {
@KyleGoslan
KyleGoslan / UIView+Extension.swift
Last active August 9, 2023 08:47
UIView extension to easily add and remove a blur (UIVisualEffectView)
import UIKit
extension UIView {
func blurView(style: UIBlurEffect.Style) {
var blurEffectView = UIVisualEffectView()
let blurEffect = UIBlurEffect(style: style)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = bounds
addSubview(blurEffectView)
}
@KyleGoslan
KyleGoslan / UIView+Extension.swift
Created July 26, 2016 10:15
UIView extension that returns a UIImage of the view.
func getSnapshotImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0)
self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: false)
let snapshotImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return snapshotImage
}
@KyleGoslan
KyleGoslan / DataLoaderProtocol
Created August 3, 2016 21:03
Protocol that loads some JSON from a file in the main bundle (Uses SwiftyJSON).
import Foundation
import SwiftyJSON
protocol DataLoader { }
extension DataLoader {
func loadJSON(fileName: String, type: String = "json") -> JSON? {
let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: type)
if let data = NSData(contentsOfFile: filePath!) {
let json = JSON(data: data)
@KyleGoslan
KyleGoslan / UIImage+Extension.swift
Created November 27, 2016 21:47
Resize UIImage Swift 3
import UIKit
extension UIImage {
func resizeWith(percentage: CGFloat) -> UIImage? {
let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: size.width * percentage, height: size.height * percentage)))
imageView.contentMode = .scaleAspectFit
imageView.image = self
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
@KyleGoslan
KyleGoslan / CalculateTime.swift
Created February 8, 2017 22:24
Calculate time to move between two points given an arbitrary speed
func getDuration(pointA: CGPoint, pointB: CGPoint, speed: CGFloat) -> TimeInterval {
let xDist = (pointB.x - pointA.x)
let yDist = (pointB.y - pointA.y)
let distance = sqrt((xDist * xDist) + (yDist * yDist));
let duration = TimeInterval(distance/speed)
return duration
}
@KyleGoslan
KyleGoslan / UIImageView+Extension.swift
Created February 20, 2017 11:12
UIImage View extension to load image from web url
extension UIImageView {
public func imageFromServerURL(urlString: String) {
URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil { return }
DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
self.image = image
@KyleGoslan
KyleGoslan / String+Extension.swift
Created February 22, 2017 11:04
Random string
extension String {
static func randomString(length: Int) -> String {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = UInt32(letters.length)
var randomString = ""
for _ in 0 ..< length {
let rand = arc4random_uniform(len)
@KyleGoslan
KyleGoslan / SKTileMapNode+Extension.swift
Created March 2, 2017 10:28
Method that loops through each tile, with a callback that return the col, row and tile definition.
extension SKTileMapNode {
func loopThroughTiles(completionClosure: (_ col: Int, _ row: Int, _ tile: SKTileDefinition?) -> ()) {
for col in 0..<numberOfColumns {
for row in 0..<numberOfRows {
completionClosure(col, row, tileDefinition(atColumn: col, row: row))
}
}
}
@KyleGoslan
KyleGoslan / DataSnapsot+Extension.swift
Last active September 28, 2018 11:32
Initilize objects from DataSnapshot that conform to the Codable protocol
import Firebase
extension DataSnapshot {
func toObject<T:Codable>() throws -> T {
var newValue = value as! [String: Any]
newValue["uid"] = key
let data = try! JSONSerialization.data(withJSONObject: newValue, options: .sortedKeys)
return try JSONDecoder().decode(T.self, from: data)
}