-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(pull-down-refresh): remove @react-spring
- Loading branch information
Showing
2 changed files
with
127 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React, { useState } from 'react'; | ||
import getScrollParent from '../_util/getScrollParent'; | ||
|
||
export function useTouch() { | ||
const [startX, setStartX] = useState(0); | ||
const [startY, setStartY] = useState(0); | ||
const [diffX, setDiffX] = useState(0); | ||
const [diffY, setDiffY] = useState(0); | ||
const start = (event: React.TouchEvent) => { | ||
const { clientX, clientY } = event.touches[0]; | ||
setStartX(clientX); | ||
setStartY(clientY); | ||
setDiffX(0); | ||
setDiffY(0); | ||
}; | ||
const move = (event: React.TouchEvent) => { | ||
const { clientX, clientY } = event.touches[0]; | ||
setDiffY(clientY - startY); | ||
setDiffX(clientX - startX); | ||
}; | ||
return { | ||
startX, | ||
startY, | ||
diffX, | ||
diffY, | ||
start, | ||
move, | ||
}; | ||
} | ||
|
||
// 缓动函数 | ||
export const easeDistance = (distance: number, pullDistance: number) => { | ||
let innerDistance = distance; | ||
if (innerDistance > pullDistance) { | ||
if (innerDistance < pullDistance * 2) { | ||
innerDistance = pullDistance + (innerDistance - pullDistance) / 2; | ||
} else { | ||
innerDistance = pullDistance * 1.5 + (innerDistance - pullDistance * 2) / 4; | ||
} | ||
} | ||
return Math.round(innerDistance); | ||
}; | ||
|
||
// 确保可滚动的父元素此时处于未滚动状态 | ||
export const isReachTop = (e: React.TouchEvent) => { | ||
const scrollParent = getScrollParent(e.target as Element); | ||
return !scrollParent || !(scrollParent as Element).scrollTop; | ||
}; |