Skip to content

Instantly share code, notes, and snippets.

@tibita11
tibita11 / ContentView.swift
Created March 30, 2024 09:32
create carousel using swiftui
import SwiftUI
struct ContentView: View {
@State private var currentIndex = 0
@GestureState private var dragOffset: CGFloat = 0
let examples = ["1", "2", "3"]
let itemPadding: CGFloat = 20
var body: some View {
GeometryReader { bodyView in
@tibita11
tibita11 / ContentView.swift
Created March 30, 2024 06:38
A view can also perform an animation when a binding value changes.
struct ContentView: View {
@State private var isTrailing = false
var body: some View {
VStack(alignment: isTrailing ? .trailing : .leading) {
Image(systemName: "box.truck")
.font(.system(size: 64))
@tibita11
tibita11 / PageControl.swift
Created March 20, 2024 00:12
use UIKit's PageViewController at SwiftUI
import UIKit
import SwiftUI
struct PageControl: UIViewRepresentable {
var numberPages: Int
@Binding var currentPage: Int
func makeCoordinator() -> Coordinator {
Coordinator(control: self)
}
@tibita11
tibita11 / FeatureCard.swift
Created March 18, 2024 23:42
gradation overlay
import SwiftUI
struct FeatureCard: View {
var landmark: Landmark
var body: some View {
landmark.featureImage?
.resizable()
.overlay { // overlayで重ねる
TextOverlay(landmark: landmark)
import SwiftUI
struct ContentView: View {
@State private var selection: CustomTab = .home
var body: some View {
VStack {
TabView(selection: $selection) {
Text("Home")
.tag(CustomTab.home)
@tibita11
tibita11 / ExtensionUIColor.swift
Created March 13, 2024 06:43
create uiimage form uicolor
func image(color: UIColor, size: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image(actions: { rendererContext in
// 描画処理
rendererContext.cgContext.setFillColor(color.cgColor) // 色を指定
rendererContext.fill(CGRect(origin: CGPoint.zero, size: size)) // 塗りつぶす
})
}
import SwiftUI
struct ContentView: View {
@State private var selection: Tab = .featured
enum Tab {
case featured
case list
@tibita11
tibita11 / EdgeInsetsOfList.swift
Created March 10, 2024 09:31
edge insets to zero
.listRowInsets(EdgeInsets())
@tibita11
tibita11 / Grouping.swift
Created March 10, 2024 08:53
dictionary grouping func
let names = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Harry", "Iris", "Jack"]
let groupedNames = Dictionary(grouping: names) { $0.first! }
print(groupedNames)
@tibita11
tibita11 / ModelData.swift
Created February 23, 2024 08:15
parse json-file
import Foundation
var landmarks: [Landmark] = load("landmarkData.json")
// 型推論を使用するためジェネリクスでもOK
func load<T: Decodable>(_ filename: String) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {