diff --git a/src/components/TemplateCard/index.tsx b/src/components/TemplateCard/index.tsx index 6210c1b..2f9b608 100644 --- a/src/components/TemplateCard/index.tsx +++ b/src/components/TemplateCard/index.tsx @@ -1,5 +1,6 @@ import { View, Text, Image } from '@tarojs/components' import { useState, useRef } from 'react' +import Taro from '@tarojs/taro' import { Template } from '../../store/types' import './index.css' @@ -11,6 +12,7 @@ interface TemplateCardProps { export default function TemplateCard({ template, onClick }: TemplateCardProps) { const [splitPosition, setSplitPosition] = useState(50) // 分割线位置百分比 const [isDragging, setIsDragging] = useState(false) + const [containerInfo, setContainerInfo] = useState(null) const containerRef = useRef(null) const handleClick = () => { @@ -19,29 +21,38 @@ export default function TemplateCard({ template, onClick }: TemplateCardProps) { } } + // 获取容器信息 + const getContainerInfo = () => { + return new Promise((resolve) => { + const query = Taro.createSelectorQuery() + query.select('.merged-image-container').boundingClientRect((rect) => { + if (rect) { + setContainerInfo(rect) + resolve(rect) + } + }).exec() + }) + } + // 处理触摸开始 - const handleTouchStart = (e: any) => { + const handleTouchStart = async (e: any) => { e.stopPropagation() setIsDragging(true) + // 获取容器信息用于后续计算 + await getContainerInfo() } // 处理触摸移动 const handleTouchMove = (e: any) => { - if (!isDragging) return + if (!isDragging || !containerInfo) return e.stopPropagation() const touch = e.touches[0] - const container = containerRef.current - if (!container || !touch) return - - // 获取容器的位置信息 - const rect = container.getBoundingClientRect() - const containerLeft = rect.left - const containerWidth = rect.width + if (!touch) return // 计算触摸点相对于容器的位置 - const touchX = touch.clientX - containerLeft - const percentage = Math.max(10, Math.min(90, (touchX / containerWidth) * 100)) + const touchX = touch.clientX - containerInfo.left + const percentage = Math.max(10, Math.min(90, (touchX / containerInfo.width) * 100)) setSplitPosition(percentage) }