Skip to content

Instantly share code, notes, and snippets.

View bittz's full-sized avatar
🤯

Michał Mosiołek bittz

🤯
View GitHub Profile
@bittz
bittz / QuadPageControl.swift
Last active February 12, 2023 16:42
UIPageControl with square dots having custom size (hack).
import UIKit
class QuadPageControl: UIPageControl {
override func layoutSubviews() {
super.layoutSubviews()
guard !subviews.isEmpty else { return }
let spacing: CGFloat = 3
@bittz
bittz / UIImage+Snapshot.swift
Created August 24, 2016 10:10
UIImage from UIView.
import UIKit
extension UIImage {
static func imageFromView(view: UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0)
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
@bittz
bittz / UIImage+Placeholder.swift
Created August 22, 2016 14:41
Shorthand for creating empty (black) UIImage.
import UIKit
extension UIImage {
static func placeholder(size: CGSize) -> UIImage {
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
CGContextSetRGBFillColor(context, 0, 0, 0, 1)
CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height))
@bittz
bittz / CustomURLCache.swift
Created August 22, 2016 14:34
Custom NSURLCache for web services which don't provide expiration info.
import Foundation
class CustomURLCache: NSURLCache {
let createdAtKey = "CreatedAt"
override func cachedResponseForRequest(request:NSURLRequest) -> NSCachedURLResponse? {
let cachedResponse = expiredCachedResponseForRequest(request)
if cachedResponse == nil {
@bittz
bittz / String+Titleized.swift
Created August 22, 2016 14:06
String with the first letter written uppercase.
import Foundation
extension String {
var titleizedString: String {
return String(characters.prefix(1)).uppercaseString + String(characters.dropFirst())
}
}
@bittz
bittz / UIViewController+Alert.swift
Last active August 31, 2016 08:17
Present basic alert with UIViewController.
import UIKit
extension UIViewController {
func presentAlert(message: String) {
if presentedViewController == nil {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(alertAction)
@bittz
bittz / SKNodeExtension.swift
Last active August 22, 2016 14:07
Remove SKNode children whose names match the search parameter.
import SpriteKit
extension SKNode {
public func removeChildrenWithName(name: String) {
enumerateChildNodesWithName(name) { (node, stop) -> Void in
node.removeFromParent()
}
}
}
@bittz
bittz / CoreGraphics+Geometry.swift
Last active April 16, 2024 10:27
Convert degrees to radians / radians to degrees in Swift.
import CoreGraphics
func degreesToRadians(degrees: CGFloat) -> CGFloat {
return degrees * CGFloat(M_PI) / 180
}
func radiansToDegress(radians: CGFloat) -> CGFloat {
return radians * 180 / CGFloat(M_PI)
}
@bittz
bittz / CGSizeExtension.swift
Last active March 21, 2016 13:10
Square CGSize
import CoreGraphics
extension CGSize {
public init(side: CGFloat) {
width = side
height = side
}
public init(side: Double) {
width = CGFloat(side)