-
Notifications
You must be signed in to change notification settings - Fork 380
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
base: master
Are you sure you want to change the base?
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 targetIndex = useSharedValue(0) | ||
const initOffset = getInitOffset() | ||
const offset = useSharedValue(initOffset) | ||
const start = useSharedValue(initOffset) |
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.
为啥需要start?
props.current && (targetIndex.value = props.current) | ||
} | ||
} | ||
}, [props.autoplay, props.current, widthState, heightState]) |
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.
Children.length变化的情况也需要考虑
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感觉不需要作为这块的依赖
}) | ||
.onUpdate((e) => { | ||
'worklet' | ||
cancelAnimation(offset) |
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.
在onBegin中cancel就行
} | ||
|
||
function createAutoPlay () { | ||
'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.
不应该声明为worklet
|
||
const totalElements = useSharedValue(children.length) | ||
const targetIndex = useSharedValue(0) | ||
const initOffset = getInitOffset() |
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.
这块的初始化可以整体放在useEffect当中进行
} | ||
} | ||
|
||
function stopLoop () { |
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.
改名叫pauseLoop吧
if (!props.circular) { | ||
// 获取下一个位置的坐标, 循环到最后一个元素,直接停止, 取消定时器 | ||
if (targetIndex.value === totalElements.value - 1) { | ||
runOnJS(stopLoop)() |
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.
不用runonjs
// 记录选中元素的索引值 | ||
const targetIndex = useSharedValue(props.current || 0) | ||
// 记录元素的偏移量 | ||
const offset = useSharedValue({ x: 0, y: 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.
最好xy都还是用一个值,因为不存在xy同时都在动的场景
if (totalElements.value !== children.length) { | ||
totalElements.value = children.length | ||
} | ||
}, [props.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.
这个在render函数里直接更新就行,不需要useEffect
easing: easeMap[easeingFunc] | ||
}, () => { | ||
offset.value = targetOffset | ||
isAutoFirst.current = 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.
这个是干嘛的
offset.value = targetOffset | ||
props.autoplay && loop() | ||
} | ||
}, [props.current, widthState, heightState]) |
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由false变为true的情况,以及autoplay和current需要分开处理,以及需要用到的props可以前面解构一次,如果有默认值也可以直接赋值。
No description provided.