Skip to content

Instantly share code, notes, and snippets.

@amandeeprehal-lab49
amandeeprehal-lab49 / AutoRotateAndFullScreen.md
Last active May 14, 2024 12:45
TWL-Lab49/AVPlayer auto rotate and programmatically enter full screen when device switched to landscape orientation with SwiftUI

iOS 14 version plus, AVPlayer that supports auto rotate and programmatically enter full screen when device switched to landscape orientation with SwiftUI

import SwiftUI
import AVKit

@available(iOS 14.0, *)
struct AutoRotate: View {
    @State private var showFullScreen = false
    let url: URL
@jfuellert
jfuellert / ScrollableView.swift
Last active December 23, 2024 02:21
A scrollable SwiftUI view, UIScrollView wrapper. ScrollableView lets you read and write content offsets for scrollview in SwiftUI, with and without animations.
import SwiftUI
struct ScrollableView<Content: View>: UIViewControllerRepresentable, Equatable {
// MARK: - Coordinator
final class Coordinator: NSObject, UIScrollViewDelegate {
// MARK: - Properties
private let scrollView: UIScrollView
var offset: Binding<CGPoint>
@darrarski
darrarski / FormattedTextField.swift
Last active September 26, 2024 06:33
SwiftUI FormattedTextField - TextField with custom display/edit formatters
import SwiftUI
public struct FormattedTextField<Formatter: TextFieldFormatter>: View {
public init(_ title: String,
value: Binding<Formatter.Value>,
formatter: Formatter) {
self.title = title
self.value = value
self.formatter = formatter
}
@brennanMKE
brennanMKE / DeviceToken.swift
Created September 25, 2019 17:54
DeviceToken Swift Playground for transforming Data into a Hex String
import Foundation
struct DeviceToken {
enum Failure: Error {
case invalidHexString
}
var data: Data
var hexString: String {
let result = data.map { String(format: "%02x", $0) }.joined().uppercased()
return result
@ppth0608
ppth0608 / TimeInterval+Project.swift
Created July 3, 2019 04:02
How to convert TimeInterval to Milliseconds or Seconds
import Foundation
extension TimeInterval {
var seconds: Int {
return Int(self.rounded())
}
var milliseconds: Int {
return Int(self * 1_000)
@mikebuss
mikebuss / decode-json-swift.swift
Created January 25, 2019 21:46
Decode [Any] and [String: Any] Swift 4
//
//
// Adapted from:
//
// Original: https://gist.github.com/loudmouth/332e8d89d8de2c1eaf81875cfcd22e24
// Adds encoding: https://github.com/3D4Medical/glTFSceneKit/blob/master/Sources/glTFSceneKit/GLTF/JSONCodingKeys.swift
// Adds fix for null inside arrays causing infinite loop: https://gist.github.com/loudmouth/332e8d89d8de2c1eaf81875cfcd22e24#gistcomment-2807855
//
struct JSONCodingKeys: CodingKey {
var stringValue: String
@proxpero
proxpero / Iso3166_1a2.swift
Last active February 20, 2024 16:44
A Swift 4.0 enum representing ISO 3166-1 alpha-2 (two letter country codes) Includes properties for country name and unicode flag.
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
enum Iso3166_1a2: String {
case af = "AF"
case ax = "AX"
case al = "AL"
case dz = "DZ"
case `as` = "AS"
case ad = "AD"
case ao = "AO"
import FirebaseFirestore
private struct Property {
let label: String
let value: Any
}
struct FirestoreModelData {
let snapshot: DocumentSnapshot
@fluffyemily
fluffyemily / geofirequeries.swift
Created October 17, 2016 16:39
Querying Geofire with Swift
let geofireRef = FIRDatabase.database().reference()
guard let geofire = GeoFire(firebaseRef: geofireRef.child("venues/")) else { return }
geofire.getLocationForKey(placeKey) { (location, error) in
if let error = error {
print("An error occurred getting the location for \(placeKey): \(error.localizedDescription)")
} else if let location = location {
print("Location for \(placeKey) is [\(location.coordinate.latitude), \(location.coordinate.longitude)]")
} else {
print("GeoFire does not contain a location for \(placeKey)")
@dedeexe
dedeexe / StringMaskFormatter.swift
Last active July 12, 2019 11:34
Simples And Fast String Mask Formatter.
//
// StringMaskFormatter.swift
// StringMaskFormatter
//
// Created by dede.exe on 10/07/16.
// It's totally base on article : "http://vojtastavik.com/2015/03/29/real-time-formatting-in-uitextfield-swift-basics/"
// And I don't mind if the original author should allow me(or not) to publish it :)... But I'll keep the reference
//
import UIKit