Skip to content

Instantly share code, notes, and snippets.

let layout = UICollectionViewCompositionalLayout(sectionProvider: {
(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
@hlung
hlung / How to connect a PS3 controller.md
Last active March 25, 2024 14:45
How to connect PS3 controller on Mac OSX, PC

How to connect PS3 controller on Mac OSX, PC, etc.

This is how you connect PS3 controller to Mac OSX, PC, etc. when previously connected to a PS3. You will need a Mini USB cable. Overcome your laziness, get up of your chair, and go get one!

A big misconception is that keep holding PS button will reset the controller's pairing. It DOES NOT! From my testings, the controller keeps paring with the last machine it was CONNECTED VIA A USB CABLE.

Here are the steps:

@hlung
hlung / CIFilter-colorAbsoluteDifference-demo.swift
Created July 21, 2023 11:22
Demonstrate how to use CIFilter.colorAbsoluteDifference() on macOS
import Cocoa
import CoreImage
import CoreImage.CIFilterBuiltins
let nextButtonImage = NSImage(named: "next button")!
let startGameButtonImage = NSImage(named: "start game button")!
computeImageDifference(inputImage: nextButtonImage, inputImage2: startGameButtonImage)
// MARK: -
@hlung
hlung / keyboardHeightObservable.swift
Last active June 11, 2023 11:41 — forked from laurilehmijoki/keyboardHeightObservable.swift
RxSwift Observable on iOS keyboard height. Updated from original. Changelog below.
import RxSwift // Version 3.2.0
import RxCocoa // Version 3.2.0
typealias KeyboardHeightInfo = (CGFloat, TimeInterval)
func keyboardHeight() -> Driver<KeyboardHeightInfo> {
return Observable
.from([
NotificationCenter.default.rx.notification(NSNotification.Name.UIKeyboardWillShow)
.map { notification -> KeyboardHeightInfo in
@hlung
hlung / UIView+TouchableViews.swift
Created January 5, 2016 06:51
Make subviews touchable even if it is outside its superview bounds.
// Make subviews touchable even if it is outside its superview bounds.
// ** Add more views here **
lazy var touchableViews: [UIView] = {
return [self.voteUpButton, self.voteDownButton, self.authorDetailButton]
}()
// Boilerplate
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
for view in touchableViews {
@hlung
hlung / PlaceholderTextView.swift
Created November 27, 2018 10:41
A UITextView subclass with placeholder text support. Swift 4.2
import UIKit
/// A UITextView subclass with placeholder text support.
/// It uses another UILabel to show the placeholder, shown when text is empty.
class PlaceholderTextView: UITextView {
lazy var placeholderLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(white: 0.5, alpha: 0.85)
label.backgroundColor = .clear
@hlung
hlung / RecursiveLazyDependency.swift
Created September 25, 2022 15:15
for medium blog
import UIKit
class MyViewController: UIViewController {
var name: String = "" {
didSet {
print("name = \(name)")
testLabel.text = name // 2
}
}
// Put this in a UITableViewCell or UICollectionViewCell subclass.
// Returns nil if point is not within the specified subviews.
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let subviews = [imageView, titleLabel, detailLabel, downloadButton]
for view in subviews {
if view.convert(view.bounds, to: self).contains(point) {
return super.hitTest(point, with: event)
}
}
return nil
let layout = UICollectionViewCompositionalLayout(sectionProvider: {
(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
// (1) setting widthDimension to half of collectionView size
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5),
heightDimension: .absolute(50))
struct LossyArray<Element: Decodable>: Decodable {
private(set) var elements: [Element]
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
var elements = [Element]()
if let count = container.count {
elements.reserveCapacity(count)
}