Skip to content

Instantly share code, notes, and snippets.

View marlonjames71's full-sized avatar
💻
Learning Swift & SwiftUI

Marlon Raskin marlonjames71

💻
Learning Swift & SwiftUI
View GitHub Profile
@sebjvidal
sebjvidal / ViewController.swift
Created November 5, 2024 15:35
UIButton Control Size for Mac Catalyst
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.role = .primary
button.controlSize = .large
@juliensagot
juliensagot / VariableBlurView.swift
Last active November 16, 2024 19:18
SwiftUI variable blur view
import Foundation
import SwiftUI
import UIKit
extension UIBlurEffect {
public static func variableBlurEffect(radius: Double, imageMask: UIImage) -> UIBlurEffect? {
let methodType = (@convention(c) (AnyClass, Selector, Double, UIImage) -> UIBlurEffect).self
let selectorName = ["imageMask:", "effectWithVariableBlurRadius:"].reversed().joined()
let selector = NSSelectorFromString(selectorName)
//
// ContentView.swift
// Emoji Animation hero picker
//
// Created by Moussa on 3/4/2024.
//
import SwiftUI
struct ContentView: View {
import Foundation
import SwiftUI
// MARK: - Custom Button Style
struct MobileMeButtonStyle: ButtonStyle {
// MARK: Metrics
@ScaledMetric private var cornerRadius = 12
@ScaledMetric private var horizontalLabelPadding = 12
@ScaledMetric private var verticalLabelPadding = 8
@dkun7944
dkun7944 / CDView.swift
Last active October 20, 2024 01:38
SwiftUI + Swift.Shader CD
//
// CDView.swift
// CD
//
// Created by Daniel Kuntz on 7/3/23.
//
import SwiftUI
struct ShapeWithHole: Shape {
@dropski
dropski / rating.swift
Created July 3, 2023 19:48
SwiftUI Simple Partial Rating
struct ContentView: View {
@State private var sliderValue: Double = 0.0
@State private var selectedIndex: Int = -1
@State private var remainder: Double = 0.0
var body: some View {
VStack {
HStack {
ForEach(0..<5) { index in
ZStack {
Image(systemName: "star.fill")
// Before 😕
let doingSomethingToOptionalView1: CGFloat? = {
guard let firstSubview = subviews.first else { return nil }
return (firstSubview.bounds.width / 2.0) + otherView.bounds.width
}()
// After 😊
let doingSomethingToOptionalView2 = subviews.first.useIfNotNil { ($0.bounds.width / 2.0) + otherView.bounds.width }
@simonbs
simonbs / AppKitTextView.swift
Created January 27, 2023 08:39
Shows how a multi-platform SwiftUI can bridge to UIKit and AppKit.
// Implementation of the view using AppKit.
#if os(macOS)
import AppKit
import SwiftUI
final class AppKitTextView: NSView {
let textView: NSTextView = {
let this = NSTextView()
this.translatesAutoresizingMaskIntoConstraints = false
return this
@nicklockwood
nicklockwood / Shape.swift
Created December 31, 2022 18:56
PolymorphicCoding.swift
import Foundation
// Here's a pretty typical scenario where you want to encode a polymorphic type -
// in this case a Shape type that can be either a Square or Circle. Swift provides
// a nice pattern for doing this in a type-safe way using enums:
struct Circle: Codable {
var radius: Int
}
@amirdew
amirdew / View+MinimumPadding.swift
Created December 22, 2022 13:20
Minimum paddings for SwiftUI views
extension View {
func minimumPadding(edges: Edge.Set = .all, _ length: CGFloat = 8) -> some View {
GeometryReader { geo in
padding(.bottom, edges.contains(.bottom) ? max(length, geo.safeAreaInsets.bottom) : 0)
.padding(.top, edges.contains(.top) ? max(length, geo.safeAreaInsets.top) : 0)
.padding(.leading, edges.contains(.leading) ? max(length, geo.safeAreaInsets.leading) : 0)
.padding(.trailing, edges.contains(.trailing) ? max(length, geo.safeAreaInsets.trailing) : 0)
.ignoresSafeArea(edges: edges)
}
}