Skip to content

Instantly share code, notes, and snippets.

View zeeshankhan's full-sized avatar
🏠
Working from home

Zeeshan Khan zeeshankhan

🏠
Working from home
View GitHub Profile
@zeeshankhan
zeeshankhan / Hacking UIView Animation Blocks.md
Created August 1, 2022 04:16 — forked from nicklockwood/Hacking UIView Animation Blocks.md
This article was originally written for objc.io issue 12, but didn't make the cut. It was intended to be read in the context of the other articles, so if you aren't familiar with concepts such as CALayer property animations and the role of actionForKey:, read the articles in that issue first.

Hacking UIView animation blocks for fun and profit

In this article, I'm going to explore a way that we can create views that implement custom Core Animation property animations in a natural way.

As we know, layers in iOS come in two flavours: Backing layers and hosted layers. The only difference between them is that the view acts as the layer delegate for its backing layer, but not for any hosted sublayers.

In order to implement the UIView transactional animation blocks, UIView disables all animations by default and then re-enables them individually as required. It does this using the actionForLayer:forKey: method.

Somewhat strangely, UIView doesn't enable animations for every property that CALayer does by default. A notable example is the layer.contents property, which is animatable by default for a hosted layer, but cannot be animated using a UIView animation block.

import UIKit
import WebKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Configure the web view for JavaScript injection
configureWebView()
// Original article here: https://www.fivestars.blog/code/swiftui-hierarchy-list.html
import SwiftUI
struct FileItem: Identifiable {
let name: String
var children: [FileItem]?
var id: String { name }
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
@State var gradientAngle: Double = 0
var colors = [
Color(UIColor.systemRed),
Color(UIColor.systemOrange),
Color(UIColor.systemYellow),
Color(UIColor.systemGreen),
@zeeshankhan
zeeshankhan / safari-reading-list-to-instapaper-csv.rb
Last active July 16, 2020 04:33
Safari Readling List to Instapaper CSV (a ruby script for macOS)
# 1. Open Terminal.app.
# 2. Type "cd Desktop" and Enter.
# 3. Type "open ~/Library/Safari/ ." and Enter.
# 4. Copy `Bookmarks.plist` file to the Desktop.
# 5. Back to the Terminal, type "ruby safari-readling-list-to-instapaper-csv.rb" and Enter.
# 6. Open Instapaper Settings (https://www.instapaper.com/user), and select "Import from Instapaper CSV."
# 7. Upload `instapaper-import.csv` from the Desktop.
require "plist"
require 'csv'
@zeeshankhan
zeeshankhan / firebase-iOS-breakdown
Created June 26, 2020 03:41 — forked from zntfdr/firebase-iOS-breakdown.swift
Firebase iOS Version breakdown
// How to:
// 1. Go in the Firebase Analytics Dashboard
// 2. Filter iOS Platform only
// 3. Scroll down, select `Device` under the "What is your audience like?" widget
// 4. Export the CSV data (top right of the corner, there's a `...` menu with Download CSV option)
// 5. Open the file and select the iOS breakdown raw data
// 6. Replace your data with the sample data in this script
// 7. Run the script in a Xcode Playground
// 8. See the terminal output
@zeeshankhan
zeeshankhan / ResultDemo.swift
Created November 7, 2016 07:15
A simple demonstration of Result
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
typealias JSONDictionary = [String: Any]
enum SomeError : Error {
case zoError
}
@zeeshankhan
zeeshankhan / result.swift
Created October 23, 2016 05:25 — forked from damianesteban/result.swift
Swift 3 updated code with simple examples from this fantastic article: http://alisoftware.github.io/swift/async/error/2016/02/06/async-errors/
// A Gist.
struct Gist {
let id: String
}
typealias JSONDictionary = [String: AnyObject]
// These methods are "helpers" to transform Data -> JSON -> Result<[Gist]>
func jsonFrom(data: Data) -> Result<AnyObject> {
let json = try! JSONSerialization.jsonObject(with: data, options: [])
@zeeshankhan
zeeshankhan / OptionalMatching.swift
Created October 23, 2016 05:23
Optional value (pattern) matching through enum
import Foundation
let x: String? = nil
let y: String? = "Hello Y"
switch (x,y) {
case (.some(let a), .some(let b)):
print("So \(a) and \(b)")
case (.none, .none):
print("None")
@zeeshankhan
zeeshankhan / Interview.swift
Last active April 13, 2016 05:33
Interview Coding Questions
// Check If a string has all unique characters, without using additional data structure.
func isUnique(string: String) -> Bool {
// what type of characters // if ascii characters
if string.characters.count > 128 {
return true
}
var cIdx = 0
for c in string.characters {