Skip to content
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

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open

Swiper重构 #1711

wants to merge 32 commits into from

Conversation

xiao19880917lu
Copy link
Collaborator

No description provided.

let renderChild = children.slice()
if (props.circular && totalElements.value > 1) {
if (totalElements.value === 2) {
renderChild = renderChild.concat(children).concat(children)
Copy link
Collaborator

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])
Copy link
Collaborator

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) => {
Copy link
Collaborator

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 }
Copy link
Collaborator

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') {
Copy link
Collaborator

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(() => {
Copy link
Collaborator

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)
Copy link
Collaborator

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()
Copy link
Collaborator

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])
Copy link
Collaborator

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) => {
Copy link
Collaborator

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)
Copy link
Collaborator

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])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Children.length变化的情况也需要考虑

Copy link
Collaborator

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)
Copy link
Collaborator

@hiyuki hiyuki Nov 21, 2024

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'
Copy link
Collaborator

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块的初始化可以整体放在useEffect当中进行

}
}

function stopLoop () {
Copy link
Collaborator

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)()
Copy link
Collaborator

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 })
Copy link
Collaborator

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])
Copy link
Collaborator

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
Copy link
Collaborator

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])
Copy link
Collaborator

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可以前面解构一次,如果有默认值也可以直接赋值。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants