Skip to content

Instantly share code, notes, and snippets.

@ilyapuchka
ilyapuchka / lint.sh
Last active July 15, 2019 15:49
Run swiftlint only on files changed from the latest commit
!/bin/sh
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
FILES=($(git ls-files -m | grep ".*\.swift$" | grep -v ".*R.generated.swift$"))
if [[ ${FILES[@]} ]]; then
export "SCRIPT_INPUT_FILE_COUNT"="${#FILES[@]}"
for i in "${!FILES[@]}"; do
export "SCRIPT_INPUT_FILE_$i"="${FILES[$i]}"
done
@fuji246
fuji246 / pinch_scale.md
Last active November 9, 2019 00:34
UIPinchGestureRecognizer Scale

I find the scale of UIPinchGestureRecognizer is confusing, so I did two experiments below, two ways to do the same thing.

Method 1

gestureRecognizer.scale start with 1.0 at the beginning of pinch (gestureRecognizer.state == .began), and gestureRecognizer.scale in later state (.changed or .end) is always based on that, for example, if the view size is view_size at the beginning of pinch (might not be the same with the original size orig_view_size), gestureRecognizer.scale always starts with 1.0, and if it becomes 2.0 later, it's size will be 2 * view_size, so the scale always based on that when the pinch starts. And we can get the scale at the beginning of pinch (gestureRecognizer.state == .began) lastScale = self.imageView.frame.width/self.imageView.bounds.size.width, so the scale of the original image now should be lastScale * gestureRecognizer.scale

  • lastScale: The scale of last round of Pinch, a round of Pinch is from state.start to state.end, and the scale is based on the o
@JohnCoates
JohnCoates / UIGestureRecognizer+Closure.swift
Created May 18, 2017 09:14
UIGestureRecognizer with closure, Swift 3
import UIKit
extension UIGestureRecognizer {
@discardableResult convenience init(addToView targetView: UIView,
closure: @escaping () -> Void) {
self.init()
GestureTarget.add(gesture: self,
closure: closure,
toView: targetView)
@twobitlabs
twobitlabs / gist:4226365
Created December 6, 2012 17:35
Blocks cheat sheet
// http://cocoawithlove.com/2009/10/ugly-side-of-blocks-explicit.html has a nice breakdown of the syntax--it helps to think of the ^ as similar to a pointer dereference symbol *
// block typedef:
typedef void(^Block)();
typedef void(^ConditionalBlock)(BOOL);
typedef NSString*(^BlockThatReturnsString)();
typedef NSString*(^ConditionalBlockThatReturnsString)(BOOL);
// block property with typedef:
@samrayner
samrayner / Realm+CascadeDeleting.swift
Last active June 24, 2021 10:38 — forked from krodak/Realm+CascadeDeleting.swift
Cascading deletion for RealmSwift
import Foundation
import RealmSwift
import Realm
//Forked from: https://gist.github.com/krodak/b47ea81b3ae25ca2f10c27476bed450c
internal protocol CascadingDeletable: RealmSwift.Object {
static var propertiesToCascadeDelete: [String] { get }
}
@mugbug
mugbug / AssertEquals+Diff.swift
Created June 10, 2021 20:29
XCTestCase wrapper for asserting equatable structs with formatted error diff message
import XCTest
// Credits: https://github.com/pointfreeco/swift-composable-architecture
//swiftlint:disable empty_string force_cast force_unwrapping unused_closure_parameter function_body_length
class MyCustomTestCase: XCTestCase {
func assertEqual<T: Equatable>(
expected: T,
actual: T
) {
@bsneed
bsneed / NSObject-extension.m
Created August 3, 2010 23:15
performSelector reimplementation with variable number of params/types and return type.
/*
You can use this to call deprecated methods without warnings (supporting old sdk's for example)
or, you can use it in place of performSelector: where you need non-object params, or multiple
params.
ie: ...
int result = 0;
int index = 12;
NSArray *array = myArray;
@JaseHadd
JaseHadd / ioslocaleidentifiers.csv
Last active March 31, 2022 08:23 — forked from jacobbubu/ioslocaleidentifiers.csv
iOS Locale Identifiers
localeIdentifier Description
eu Basque
hr_BA Croatian (Bosnia & Herzegovina)
en_CM English (Cameroon)
rw_RW Kinyarwanda (Rwanda)
en_SZ English (Swaziland)
tk_Latn Turkmen (Latin)
he_IL Hebrew (Israel)
ar Arabic
uz_Arab Uzbek (Arabic)
@steipete
steipete / PSPDFThreadSafeMutableDictionary.m
Last active December 10, 2022 09:37
Simple implementation of a thread safe mutable dictionary. In most cases, you want NSCache instead, but it can be useful in situations where you want to manually control what is evicted from the cache in low memory situations.**Warning:** I only use this for setting/getting keys. Enumeration is not thread safe here and will still throw exception…
//
// PSPDFThreadSafeMutableDictionary.m
//
// Copyright (c) 2013 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is