jasudev / fabulaitemsprovider Goto Github PK
View Code? Open in Web Editor NEWYou can share and communicate with developers around the world through the Fabula app.
Home Page: https://apps.apple.com/app/id1591155142
License: MIT License
You can share and communicate with developers around the world through the Fabula app.
Home Page: https://apps.apple.com/app/id1591155142
License: MIT License
//
// P277_DragAndResize.swift
//
//
// Created by tgeisse on 12/5/22.
//
import SwiftUI
public struct P277_DragAndResize: View {
public init() { }
public var body: some View {
DragAndResize()
}
}
fileprivate struct DragAndResize: View {
var body: some View {
DraggableAndResizableView {
Image(systemName: "atom")
.resizable()
.symbolRenderingMode(.palette)
.foregroundStyle(.yellow, .mint)
}
}
}
fileprivate struct ResizePoint: Identifiable {
let id = UUID()
let x: RelativeLocation
let y: RelativeLocation
enum RelativeLocation {
case zero
case middle
case max
func value(from: CGFloat) -> CGFloat {
switch self {
case .zero: return 0
case .middle: return from / 2
case .max: return from
}
}
}
}
fileprivate struct DraggableAndResizableView<Content: View>: View {
@ViewBuilder let content: () -> Content
private let initialSize: CGSize
@State private var size: CGSize
@State private var offset: CGPoint = .zero
private let resizablePoints: [ResizePoint] = [ // position is relative to their place around the view
ResizePoint(x: .zero, y: .zero), ResizePoint(x: .middle, y: .zero), ResizePoint(x: .max, y: .zero),
ResizePoint(x: .zero, y: .middle), ResizePoint(x: .max, y: .middle),
ResizePoint(x: .zero, y: .max), ResizePoint(x: .middle, y: .max), ResizePoint(x: .max, y: .max),
]
init(initialSize: CGSize = CGSize(width: 200, height: 200),
content: @escaping () -> Content) {
self.content = content
self.initialSize = initialSize
self.size = initialSize
}
var body: some View {
GeometryReader { screen in
ZStack {
content()
.padding(8)
.frame(width: size.width, height: size.height)
.offset(x: offset.x,
y: offset.y)
.gesture(ViewDragGesture(point: $offset))
ForEach(resizablePoints) { resizePoint in
let pointLocation = CGPoint(x: offset.x - size.width / 2
+ resizePoint.x.value(from: size.width),
y: offset.y - size.height / 2
+ resizePoint.y.value(from: size.height))
Rectangle()
.stroke(.primary)
.background(Rectangle().fill(.gray))
.frame(width: 6, height: 6)
.offset(x: pointLocation.x,
y: pointLocation.y)
.gesture(ResizePointDragGesture(resizePoint: resizePoint,
offset: $offset,
size: $size)
)
}
}
.onAppear {
reset(screenSize: screen.size)
}
.onTapGesture(count: 2) {
reset(screenSize: screen.size)
}
}
.ignoresSafeArea()
}
private func reset(screenSize : CGSize) {
size = initialSize
offset.x = screenSize.width / 2 - size.width / 2
offset.y = screenSize.height / 2 - size.height / 2
}
}
fileprivate struct ViewDragGesture: Gesture {
@binding var point: CGPoint
@State private var startingPoint: CGPoint? = nil
var body: some Gesture {
DragGesture()
.onChanged { value in
if startingPoint == nil {
startingPoint = point
}
point.x = startingPoint!.x + value.translation.width
point.y = startingPoint!.y + value.translation.height
}
.onEnded { _ in
startingPoint = nil
}
}
}
fileprivate struct ResizePointDragGesture: Gesture {
let resizePoint: ResizePoint
@binding var offset: CGPoint
@binding var size: CGSize
@State private var startingSize: CGSize? = nil
@State private var startingOffset: CGPoint? = nil
var body: some Gesture {
DragGesture(coordinateSpace: .global)
.onChanged { value in
if startingSize == nil {
startingSize = size
}
if startingOffset == nil {
startingOffset = offset
}
switch resizePoint.x {
case .zero: size.width = startingSize!.width - value.translation.width
case .max: size.width = startingSize!.width + value.translation.width
default: break
}
let widthMin: CGFloat = 10
let widthMinReached = size.width <= widthMin
size.width = max(widthMin, size.width)
switch resizePoint.y {
case .zero: size.height = startingSize!.height - value.translation.height
case .max: size.height = startingSize!.height + value.translation.height
default: break
}
let heightMin: CGFloat = 10
let heightMinReached = size.height <= heightMin
size.height = max(heightMin, size.height)
if !widthMinReached && resizePoint.x == .zero {
offset.x = startingOffset!.x + value.translation.width
}
if !heightMinReached && resizePoint.y == .zero {
offset.y = startingOffset!.y + value.translation.height
}
}
.onEnded { _ in
startingSize = nil
startingOffset = nil
}
}
}
struct P277_DragAndResize_Previews: PreviewProvider {
static var previews: some View {
P277_DragAndResize()
}
}
Is this project being maintained or is there a plan to bump up the min deployment target?
Can you add an example for a time duration picker. Like somehow two wheel picker next to each other. One for the hours and the other one for minutes.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.