Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
maxcampolo / NativeWebView.swift
Created July 28, 2016 13:58
WKWebView setup to make a web page adopt native behavior.
import WebKit
class NativeWebViewController: UIViewController {
let viewportScriptString = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); meta.setAttribute('initial-scale', '1.0'); meta.setAttribute('maximum-scale', '1.0'); meta.setAttribute('minimum-scale', '1.0'); meta.setAttribute('user-scalable', 'no'); document.getElementsByTagName('head')[0].appendChild(meta);"
let disableSelectionScriptString = "document.documentElement.style.webkitUserSelect='none';"
let disableCalloutScriptString = "document.documentElement.style.webkitTouchCallout='none';"
override func viewDidLoad() {
// 1 - Make user scripts for injection
@maxcampolo
maxcampolo / AVThumbnailGen.swift
Last active November 26, 2023 15:24
Generate images from AVAsset with AVAssetImageGenerator and from AVPlayerItem with AVPlayerItemVideoOutput
/// Generate thumbnail with AVAssetImageGenerator
func generateThumbnailFromAsset(asset: AVAsset, forTime time: CMTime) -> UIImage {
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
var actualTime: CMTime = kCMTimeZero
do {
let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: &actualTime)
let image = UIImage(CGImage: imageRef)
return image
} catch let error as NSError {
@maxcampolo
maxcampolo / UIViewController+Tag.swift
Last active July 4, 2023 07:12
Extension that adds a tag property to UIViewController
/*
*
* This class provides a UIViewController extension which adds a tag property to all UIViewController subclasses.
*
*/
import Foundation
private var tagAssociationKey: UInt8 = 0
@maxcampolo
maxcampolo / DropShadow.swift
Created March 4, 2016 19:45
Add a drop shadow to a UIView
let shadowPath = UIBezierPath(rect: self.myView.layer.bounds)
self.myView.layer.masksToBounds = false
self.myView.layer.shadowColor = UIColor.blackColor().CGColor
self.myView.layer.shadowOffset = CGSizeMake(0.0, -2.0)
self.myView.layer.shadowOpacity = 0.3
self.myView.layer.shadowRadius = 1.0
self.myView.layer.shadowPath = shadowPath.CGPath
@maxcampolo
maxcampolo / ViewController+IsVisible.swift
Created June 13, 2016 15:21
Check if a UIViewController is currently visible.
extension UIViewController {
func isVisible() -> Bool {
return self.isViewLoaded() && self.view.window != nil
}
}
@maxcampolo
maxcampolo / UIView+Shadow.swift
Created August 29, 2016 16:39
Extension to add a shadow to a UIView
extension UIView {
/**
Set a shadow on a UIView.
- parameters:
- color: Shadow color, defaults to black
- opacity: Shadow opacity, defaults to 1.0
- offset: Shadow offset, defaults to zero
- radius: Shadow radius, defaults to 0
- viewCornerRadius: If the UIView has a corner radius this must be set to match
@maxcampolo
maxcampolo / SwiftSynchronized.swift
Created April 19, 2016 19:07
Mutual exclusion in Swift (equivalent to @synchronized in obj-c)
/**
Function that locks an object while it is being acted upon in the closure parameter.
Rethrows allows the function to throw an error for throwing closures or not for non-throwing closures (do not need `try` for non-throwing closures).
parameter lock: Object to be sync'd
parameter closure: Code of critical section
*/
public func synchronized<T>(lock: AnyObject, @noescape closure: () throws -> T) rethrows -> T {
objc_sync_enter(lock)
defer {
objc_sync_exit(lock)
@maxcampolo
maxcampolo / UIView+PassThrough.swift
Last active December 14, 2020 11:07
UIView subclass that is transparent to all touch events besides those on eligible child views.
import UIKit
/**
UIView subclass that is transparent to all touch events besides those on eligible child views.
*/
class TKPassThroughView: UIView {
// MARK - Touch Handling
@maxcampolo
maxcampolo / UIResponder+FirstResponder.swift
Created August 21, 2017 14:08
UIResponder that finds the current first responder of an iOS application.
//
// UIResponder+FirstResponder.swift
// Thor
//
// Created by Max Campolo on 8/20/17.
// Copyright © 2017 Max Campolo. All rights reserved.
//
import Foundation
import UIKit
@maxcampolo
maxcampolo / AnimatedSublayer.swift
Last active May 31, 2018 23:15
Animate position of a sublayer when animating a UIView.
public override func layoutSubviews() {
super.layoutSubviews()
// If the view is animating apply the animation to the sublayer
CATransaction.begin()
if let animation = layer.animationForKey("position") {
CATransaction.setAnimationDuration(animation.duration)
CATransaction.setAnimationTimingFunction(animation.timingFunction)
} else {
CATransaction.disableActions()