-
Notifications
You must be signed in to change notification settings - Fork 381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swiper重构 #1711
Swiper重构 #1711
Conversation
let renderChild = children.slice() | ||
if (props.circular && totalElements.value > 1) { | ||
if (totalElements.value === 2) { | ||
renderChild = renderChild.concat(children).concat(children) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
添加节点需要cloneElement
renderChild = renderChild.concat(children).concat(children) | ||
} else { | ||
// 最前加两个 | ||
renderChild.unshift(children[totalElements.value - 1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
前面不需要加元素
], { layoutRef: layoutRef }) | ||
|
||
function onWrapperLayout (e: LayoutChangeEvent) { | ||
nodeRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
layout里面可以直接获取高宽,不需要measure
|
||
function onWrapperLayout (e: LayoutChangeEvent) { | ||
nodeRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => { | ||
layoutRef.current = { x, y, width, height, offsetLeft, offsetTop } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要
} | ||
const attr = dir.value === 'x' ? 'width' : 'height' | ||
changeState[attr] = changeState[attr] - previousMargin - nextMargin | ||
if (dir.value === 'x') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不应该都去设置么
if (totalElements.value <= 1) return null | ||
const dots: Array<ReactNode> = [] | ||
for (let i = 0; i < totalElements.value; i++) { | ||
const dotStyle = useAnimatedStyle(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不应该在for中使用hooks,假如elements数量更新了就炸了
} | ||
|
||
function handlerInterval (loopv: boolean, oldLoop: boolean | null) { | ||
clearInterval(intervalId.current) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥有一个timeout有一个interval?
intervalId.current = setInterval(() => { | ||
// canLoop变化比较快的情况下 | ||
if (canLoop.value) { | ||
createAutoPlay() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个东西为啥要声明为worklet?看上去是个纯js函数
if (!isInit && props.bindchange) { | ||
runOnJS(handleSwiperChange)(newIndex) | ||
} | ||
}, [targetIndex.value]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不需要写deps把
} | ||
}, [targetIndex.value]) | ||
|
||
useAnimatedReaction(() => canLoop.value, (loopv, oldLoop) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
canLoop是干啥的
} | ||
}, []) | ||
|
||
const reachBoundary = useCallback((eventData: EventDataType) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以把这一堆函数全都放在下面的useMemo里就行了,就不需要这么多useCallback了
const nextMarginShared = useSharedValue(nextMargin) | ||
// 默认前后补位的元素个数 | ||
const patchEleNum = props.circular ? (preMargin ? 2 : 1) : 0 | ||
const patchElementNum = useSharedValue(patchEleNum) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没有更新逻辑
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
patchElmNum/patchElmNumShared
// 默认前后补位的元素个数 | ||
const patchEleNum = props.circular ? (preMargin ? 2 : 1) : 0 | ||
const patchElementNum = useSharedValue(patchEleNum) | ||
const circular = useSharedValue(props.circular || false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没有更新
} | ||
}) | ||
|
||
const renderPagination = useCallback(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不需要useCallback
useEffect(() => { | ||
let patchStep = 0 | ||
if (preMargin !== previousMargin.value) { | ||
patchStep = preMargin - previousMargin.value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+=
const { textProps } = splitProps(props) | ||
const preMargin = props['previous-margin'] ? parseInt(props['previous-margin']) : 0 | ||
const nextMargin = props['next-margin'] ? parseInt(props['next-margin']) : 0 | ||
const previousMargin = useSharedValue(preMargin) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preMarginShared
} | ||
// resumeLoop在worklet中调用 | ||
function resumeLoop () { | ||
if (props.autoplay && childrenLength.value > 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
autoplay也得是个sharedValue
return targetOffset | ||
} | ||
// loop在JS线程中调用,createAutoPlay + useEffect中 | ||
function loop () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加个clearTimeout
return | ||
} | ||
const targetOffset = getOffset(props.current || 0, step.value) | ||
if (props.current !== undefined && props.current !== currentIndex.value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
封装updateCurrent函数在useEffect和onLayout中复用
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
判断条件可以改为targetOffset!==offset.value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
内部基于props.current!==currentIndex.value决定是否使用动画及更新currentIndex.value
if (!step.value) { | ||
return | ||
} | ||
if (props.autoplay && children.length > 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
封装updateAutoplay函数在useEffect和onLayout中复用
duration: easeDuration, | ||
easing: easeMap[easeingFunc] | ||
}, () => { | ||
if (touchfinish.value !== false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
验证一下回调在cancelAnimation时的执行时机,是立刻执行还是得等duration结束再执行
return | ||
} | ||
const { isBoundary, resetOffset } = reachBoundary(eventData) | ||
if (isBoundary && circular.value && !touchfinish.value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
touchmove时理论上不需要判断touchfinish吧
moveTime.value = new Date().getTime() | ||
}) | ||
.onTouchesMove((e) => { | ||
'worklet' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(touchfinish.value) return
const gestureHandler = useMemo(() => { | ||
const gesturePan = Gesture.Pan() | ||
.onBegin((e) => { | ||
'worklet' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(!step.value) return
preAbsolutePos.value = touchEventData[strAbso] | ||
}) | ||
.onTouchesUp((e) => { | ||
'worklet' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(touchfinish.value) return
const realHeight = dir === 'y' ? height - preMargin - nextMargin : height | ||
const iStep = dir === 'x' ? realWidth : realHeight | ||
step.value = iStep | ||
if (touchfinish.value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个判断干掉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
判断step.value!==iStep再执行更新逻辑
return (<SwiperContext.Provider value={contextValue}>{arrChildren}</SwiperContext.Provider>) | ||
} | ||
|
||
const createAutoPlay = useCallback(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useMemo(()=>{
function createAutoplay(){}
function loop(){}
function resumeLoop(){}
function pauseLoop(){}
return {
loop,
resumeLoop,
pauseLoop
}
})
import { GestureDetector, Gesture } from 'react-native-gesture-handler' | ||
import Animated, { useAnimatedStyle, useSharedValue, withTiming, Easing, runOnJS, useAnimatedReaction, cancelAnimation } from 'react-native-reanimated' | ||
|
||
import React, { JSX, forwardRef, useRef, useEffect, ReactNode, ReactElement, useCallback, useMemo } from 'react' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useCallback可以删掉了
const nextMargin = props['next-margin'] ? parseInt(props['next-margin']) : 0 | ||
const previousMarginShared = useSharedValue(preMargin) | ||
const nextMarginShared = useSharedValue(nextMargin) | ||
const autoplayShared = useSharedValue(autoplay || false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
props的默认值统一在上面给吧
const { textProps } = splitProps(props) | ||
const preMargin = props['previous-margin'] ? parseInt(props['previous-margin']) : 0 | ||
const nextMargin = props['next-margin'] ? parseInt(props['next-margin']) : 0 | ||
const previousMarginShared = useSharedValue(preMargin) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改为preMarginShared吧,和上面的preMargin对齐
}, [props.current]) | ||
|
||
useEffect(() => { | ||
autoplayShared.value = autoplay || false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要默认值,统一在解构props时赋予默认值
const { textStyle } = splitStyle(normalStyle) | ||
const { textProps } = splitProps(props) | ||
const preMargin = props['previous-margin'] ? parseInt(props['previous-margin']) : 0 | ||
const nextMargin = props['next-margin'] ? parseInt(props['next-margin']) : 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用global.__formatValue来获取数值,可以处理rpx
No description provided.