Skip to content

Instantly share code, notes, and snippets.

@wtpalexander
wtpalexander / PagerView.swift
Created March 10, 2021 15:32
Custom PagerView to replace PageTabViewStyle, that currently has performance issues with complex views. Originally created by @mecid (https://gist.github.com/mecid/e0d4d6652ccc8b5737449a01ee8cbc6f), I've changed the animation to work just like the PageTabViewStyle when changed using `withAnimation`.
//
// PagerView.swift
//
// Originally Created by Majid Jabrayilov on 12/5/19.
// Modified by Will Alexander on 10/3/2021
// Copyright © 2019 Majid Jabrayilov. All rights reserved.
// Copyright © 2020 appstrm. All rights reserved.
//
import SwiftUI
@EnesKaraosman
EnesKaraosman / MailView.swift
Created December 10, 2020 05:48
Mail composer in SwiftUI
// https://stackoverflow.com/questions/56784722/swiftui-send-email
public struct MailView: UIViewControllerRepresentable {
@Environment(\.presentationMode) var presentation
@Binding var result: Result<MFMailComposeResult, Error>?
public var configure: ((MFMailComposeViewController) -> Void)?
public class Coordinator: NSObject, MFMailComposeViewControllerDelegate {
@Binding var presentation: PresentationMode
@mecid
mecid / Calendar.swift
Last active May 5, 2024 08:43
SwiftUI Calendar view using LazyVGrid
import SwiftUI
extension Calendar {
func generateDates(
inside interval: DateInterval,
matching components: DateComponents
) -> [Date] {
var dates: [Date] = []
dates.append(interval.start)
@mecid
mecid / PagerView.swift
Last active December 10, 2023 19:24
PagerView in SwiftUI
//
// PagerView.swift
//
// Created by Majid Jabrayilov on 12/5/19.
// Copyright © 2019 Majid Jabrayilov. All rights reserved.
//
import SwiftUI
struct PagerView<Content: View>: View {
let pageCount: Int
@inorganik
inorganik / randomLocations.swift
Created June 22, 2017 14:37
Return a cluster of random locations around a given location
import Foundation
import CoreLocation
struct randomLocations {
// create random locations (lat and long coordinates) around user's location
func getMockLocationsFor(location: CLLocation, itemCount: Int) -> [CLLocation] {
func getBase(number: Double) -> Double {
return round(number * 1000)/1000
@policante
policante / UIView+Shimmer.swift
Created February 6, 2017 14:09
Shimmer effect to UIView
extension UIView {
func startShimmering(){
let light = UIColor.white.cgColor
let alpha = UIColor.white.withAlphaComponent(0.7).cgColor
let gradient = CAGradientLayer()
gradient.colors = [alpha, light, alpha, alpha, light, alpha]
gradient.frame = CGRect(x: -self.bounds.size.width, y: 0, width: 3 * self.bounds.size.width, height: self.bounds.size.height)
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
//
// InAppManager.swift
//
// Created by Ellina Kuznetcova on 12/10/2016.
// Copyright © 2016 Flatstack. All rights reserved.
//
import Foundation
import StoreKit
@zacwest
zacwest / ios-font-sizes.swift
Last active May 6, 2024 13:04
iOS default font sizes - also available on https://www.iosfontsizes.com
let styles: [UIFont.TextStyle] = [
// iOS 17
.extraLargeTitle, .extraLargeTitle2,
// iOS 11
.largeTitle,
// iOS 9
.title1, .title2, .title3, .callout,
// iOS 7
.headline, .subheadline, .body, .footnote, .caption1, .caption2,
]
@tbergeron
tbergeron / getAvgTime.swift
Last active June 15, 2022 15:09
Calculate average time from multiple NSDate in Swift
func getAvgTime(results: Array<NSDate>) -> String {
var totalHours = 0.0
var totalMinutes = 0.0
var avgTime = ""
// sum all hours & minutes together
for result in results {
let hours = Double(NSCalendar.currentCalendar().component(NSCalendarUnit.Hour, fromDate: result))
let minutes = Double(NSCalendar.currentCalendar().component(NSCalendarUnit.Minute, fromDate: result))