import React, { useRef, useState, useMemo } from "react"; import { View, Animated,…
- User
import React, { useRef, useState, useMemo } from "react"; import { View, Animated, PanResponder, Dimensions, Text } from "react-native";
const DragAndDropExample = () => { // Rastgele sayı dizisi oluşturan yardımcı fonksiyon const generateRandomNumbers = (size) => { const numbers = []; for (let i = 0; i < size; i++) { numbers.push(Math.floor(Math.random() * 10)); // 0-9 arası rastgele sayı } return numbers; };
const squareSize = 50; const gridSize = 5;
// Ekran boyutlarını al const { width: screenWidth, height: screenHeight } = Dimensions.get("window");
// Grid boyutuna göre merkezi hesapla const gridWidth = squareSize * gridSize; const gridHeight = squareSize * gridSize; const gridXOffset = (screenWidth - gridWidth) / 2; const gridYOffset = (screenHeight - gridHeight) / 2 - 150;
// Hedef karelerin konumları (5x5 grid) const targetPositions = []; for (let row = 0; row < gridSize; row++) { for (let col = 0; col < gridSize; col++) { targetPositions.push({ x: gridXOffset + col * squareSize, y: gridYOffset + row * squareSize, }); } }
// Sürüklenebilir blokların tanımları const draggableBlocks = useMemo(() => [ { size: 2, color: "red", orientation: "horizontal", numbers: generateRandomNumbers(2) }, { size: 3, color: "green", orientation: "horizontal", numbers: generateRandomNumbers(3) }, { size: 4, color: "orange", orientation: "horizontal", numbers: generateRandomNumbers(4) }, { size: 2, color: "purple", orientation: "vertical", numbers: generateRandomNumbers(2) }, { size: 3, color: "blue", orientation: "vertical", numbers: generateRandomNumbers(3) }, { size: 4, color: "pink", orientation: "vertical", numbers: generateRandomNumbers(4) }, ], []); // Boş bağımlılık dizisi, sadece bir kez çalıştırılmasını sağlar
const [insideTargets, setInsideTargets] = useState( draggableBlocks.map(() => false) );
const filledCellsRef = useRef(draggableBlocks.map(() => [])); const pans = useRef(draggableBlocks.map(() => new Animated.ValueXY())).current;
// zIndexes state'ini oluştur const [zIndexes, setZIndexes] = useState(draggableBlocks.map(() => 1));
// Blokların başlangıç pozisyonları const initialPositions = (() => { let currentX = gridXOffset; let currentY = gridYOffset + gridHeight + 20; const positions = []; let maxHeightInRow = 0;
draggableBlocks.forEach((block, index) => { const blockWidth = block.orientation === "horizontal" ? squareSize * block.size : squareSize; const blockHeight = block.orientation === "vertical" ? squareSize * block.size : squareSize; // Eğer blok mevcut satıra sığmıyorsa, yeni satıra geç if (currentX + blockWidth > gridXOffset + gridWidth) { currentX = gridXOffset; currentY += maxHeightInRow + 10; maxHeightInRow = 0; } positions.push({ x: currentX, y: currentY, }); currentX += blockWidth + 10; // 10 piksel boşluk bırak maxHeightInRow = Math.max(maxHeightInRow, blockHeight); }); return positions;})();
- Assistant