Skip to content

Commit

Permalink
reduced the number of shapes generated on drag
Browse files Browse the repository at this point in the history
  • Loading branch information
mikelikesdesign committed Sep 12, 2024
1 parent abf6396 commit afec713
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions shapes/shapes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"shapes/Preview Content\"";
DEVELOPMENT_TEAM = 6M7A3RK3QY;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
Expand Down Expand Up @@ -300,6 +301,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"shapes/Preview Content\"";
DEVELOPMENT_TEAM = 6M7A3RK3QY;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
Expand Down
Binary file not shown.
26 changes: 19 additions & 7 deletions shapes/shapes/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import SwiftUI

struct ContentView: View {
@State private var shapes: [ShapeData] = []
@State private var isDragging = false
@State private var lastShapePosition: CGPoint?
let animationDuration: Double = 0.7
let minDistanceBetweenShapes: CGFloat = 15 // Reduced from 20
let maxShapes: Int = 50 // Increased from 30

var body: some View {
ZStack {
Expand All @@ -28,20 +30,26 @@ struct ContentView: View {
.gesture(
DragGesture(minimumDistance: 0)
.onChanged { value in
if !isDragging {
isDragging = true
if shouldAddShape(at: value.location) {
addShape(at: value.location)
}
addShape(at: value.location)
}
.onEnded { _ in
isDragging = false
}
)
}

func shouldAddShape(at location: CGPoint) -> Bool {
guard let lastPosition = lastShapePosition else { return true }
return distance(from: lastPosition, to: location) >= minDistanceBetweenShapes
}

func addShape(at location: CGPoint) {
let newShape = ShapeData(position: location)
shapes.append(newShape)
lastShapePosition = location

if shapes.count > maxShapes {
shapes.removeFirst()
}

withAnimation(.easeOut(duration: animationDuration)) {
if let index = shapes.firstIndex(where: { $0.id == newShape.id }) {
Expand All @@ -54,6 +62,10 @@ struct ContentView: View {
shapes.removeAll { $0.id == newShape.id }
}
}

func distance(from p1: CGPoint, to p2: CGPoint) -> CGFloat {
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2))
}
}

struct ShapeData: Identifiable {
Expand Down

0 comments on commit afec713

Please sign in to comment.