Skip to content

Instantly share code, notes, and snippets.

View saoudrizwan's full-sized avatar

Saoud Rizwan saoudrizwan

View GitHub Profile
@saoudrizwan
saoudrizwan / TapGestureRecognizerWithoutSelector.swift
Last active March 4, 2024 06:45
Easily create tap gesture recognizers for any view using closures as actions instead of selectors.
import UIKit
extension UIView {
// In order to create computed properties for extensions, we need a key to
// store and access the stored property
fileprivate struct AssociatedObjectKeys {
static var tapGestureRecognizer = "MediaViewerAssociatedObjectKey_mediaViewer"
}
@saoudrizwan
saoudrizwan / TouchUpInsideViews.swift
Last active October 29, 2023 14:16
Using a long press gesture recognizer, you can recreate a 'touch up inside' button effect on any view.
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(upgradeAlertViewOtherUpgradesLongPressHandler))
longPress.minimumPressDuration = 0
var longPressGRStartPoint: CGPoint?
var didCancelLongPressGR = false
func viewTouched(sender: UILongPressGestureRecognizer) {
let currentPoint = sender.location(in: self.view)
switch sender.state {
case .began:
import UIKit
class ResponsiveView: UIView {
override var canBecomeFirstResponder: Bool {
return true
}
}
class ViewController: UIViewController {
@saoudrizwan
saoudrizwan / TapticEngineHapticSignals.swift
Last active May 21, 2023 22:21
Example of using Taptic Engine haptic signals on iOS
import AudioToolbox.AudioServices
// 'Peek' feedback (weak boom)
let peek = SystemSoundID(1519)
AudioServicesPlaySystemSound(peek)
// 'Pop' feedback (strong boom)
let pop = SystemSoundID(1520)
AudioServicesPlaySystemSound(pop)
struct Post: Codable {
let user: Int
let body: String
enum CodingKeys: String, CodingKey {
case user = "userId"
case body
}
}
@saoudrizwan
saoudrizwan / Storage.swift
Last active October 27, 2021 01:51
Helper class to easily store and retrieve Codable structs from/to disk. https://medium.com/@sdrzn/swift-4-codable-lets-make-things-even-easier-c793b6cf29e1
import Foundation
public class Storage {
fileprivate init() { }
enum Directory {
// Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents directory and will be automatically backed up by iCloud.
case documents
class LinkResponsiveTextView: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
self.delaysContentTouches = false
// required for tap to pass through on to superview & for links to work
self.isScrollEnabled = false
self.isEditable = false
self.isUserInteractionEnabled = true
@saoudrizwan
saoudrizwan / os_log.swift
Last active October 5, 2019 05:11
Easy way to log file name, function, and line number to Console using Unified Logging in Swift. See String format specifiers: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
import os.log
os_log("[%{public}@/%{public}@:%{public}@] This is an error message", log: OSLog(subsystem: "my.system", category: "Networking"), type: OSLogType.error, ("\(#file)" as NSString).lastPathComponent, "\(#function)", "\(#line)")
// Alternatively use a global helper method
enum LogCategory: String {
case `default` = "Default"
case networking = "Networking"
}
@saoudrizwan
saoudrizwan / TaskManager.swift
Last active February 27, 2019 17:13
Handle multiple network requests with the same URL efficiently with a shared task manager
import Foundation
class TaskManager {
static let shared = TaskManager()
let session = URLSession(configuration: .default)
typealias completionHandler = (Data?, URLResponse?, Error?) -> Void
var tasks = [URL: [completionHandler]]()
let sameUrl = URL(string: "https://i.redd.it/dj4bz294zqhz.png")!
TaskManager.shared.dataTask(with: sameUrl) { (data, response, error) in
// ...
}
TaskManager.shared.dataTask(with: sameUrl) { (data, response, error) in
// ...
}