Skip to content

Instantly share code, notes, and snippets.

@heinrisch
heinrisch / UICollectionview.swift
Created Mar 21, 2017
Nicer register class and dequeue
View UICollectionview.swift
extension UICollectionView {
func dequeueCell<T: UICollectionViewCell>(_ indexPath: IndexPath) -> T {
let reuseIdentifier = T.classForCoder().description()
guard let cell = dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? T else {
fatalError("Could not dequeue cell with identifier \(reuseIdentifier)")
}
return cell
}
View UINavigationController+StatusbarColorChange.swift
import Foundation
extension UINavigationController {
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
if self !== UINavigationController.self {
return
@heinrisch
heinrisch / Lookups.kt
Last active May 9, 2016
Easy lookup creation from lists
View Lookups.kt
import java.util.*
// Already in Kotlin std lib: list.mapTo(mutableSetOf(), keyGenerator)
fun <K, V> List<V>.createLookupSet(keyGenerator: (V) -> K) : HashSet<K> {
return fold(HashSet<K>(), {set, value -> set.add(keyGenerator(value)); set})
}
// Already in Kotlin std lib: list.associateBy(keyGenerator)
fun <K, V> List<V>.createLookupMap(keyGenerator: (V) -> K) : HashMap<K, V> {
return fold(HashMap<K, V>(), {map, value -> map.put(keyGenerator(value), value); map})
@heinrisch
heinrisch / RecyclingMergeAdapter.kt
Last active Jan 25, 2017
Now it works with animations! Reset the index before calling notifyItemXXX
View RecyclingMergeAdapter.kt
package co.leftorright.android.util
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
/**
* Created by henrik on 02/08/16.
*/
View lazyview.kt
private class LazyView<T>(view : View, res : Int) : ReadWriteProperty<Any?, T> {
private var mView: View = view
private var res : Int = res
private var mValue: T? = null
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
mValue = mValue ?: mView.findViewById(res) as T
return mValue!!
}
View hitTest.swift
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let errorMargin : CGFloat = 30;
let largerFrame = CGRectMake(0 - errorMargin, 0 - errorMargin, frame.size.width + 2*errorMargin, frame.size.height + 2*errorMargin);
return largerFrame.contains(point) ? self : nil
}
@heinrisch
heinrisch / about.swift
Created Aug 7, 2015
Very useful operator for not being precise..
View about.swift
prefix operator ~ {}
prefix func ~ (number: Float) -> Float {
let random = Float(arc4random()) / Float(UINT32_MAX)
return number + (0.1*number - 0.2*number*random) // +-10%
}