-
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
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是干啥的
renderChild = [lastChild].concat(renderChild).concat([firstChild]) | ||
} | ||
} | ||
const arrChilds = renderChild.map((child, index) => { |
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.
arrChildren
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.
这个itemAnimatedStyle是不是可以在swiper-item内生成就可以
const newChild = React.cloneElement(child, { | ||
itemIndex: index, | ||
customStyle: [itemAnimatedStyles, extraStyle], | ||
key: 'page' + index |
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.
不需要自己去定义key,有可能会覆盖用户定义的key
|
||
useAnimatedReaction(() => currentIndex.value, (newIndex, preIndex) => { | ||
// 这里必须传递函数名, 直接写()=> {}形式会报 访问了未sharedValue信息 | ||
const isInit = !preIndex && newIndex === 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.
isInit的的判断为啥需要newIndex === 0,用户可以直接传props定义非0的初始index吧
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.
currentIndex基于props.current进行初始化
} | ||
} | ||
|
||
function getOffset (index?: 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.
index参数改为必传吧
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.
getOffset不需要是worklet
const step = useSharedValue(initStep) | ||
const totalElements = useSharedValue(children.length) | ||
// 记录选中元素的索引值 | ||
const currentIndex = useSharedValue(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.
基于props.current初始化
// 记录选中元素的索引值 | ||
const currentIndex = useSharedValue(0) | ||
// 记录元素的偏移量 | ||
const offset = useSharedValue(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.
基于props.current调用getOffset进行初始化
} | ||
|
||
function renderItems () { | ||
const itemAnimatedStyles = 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.
这个可以直接在swiperItem中的useAnimatedStyle进行
|
||
function loop () { | ||
timerId.current = setTimeout(() => { | ||
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.
无效的箭头函数包裹
runOnUI(() => { | ||
offset.value = getOffset(currentIndex.value) | ||
})() | ||
pauseLoop() |
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 currentIndex = useSharedValue(props.current || 0) | ||
// 确保在UI线程能拿到最新的值 | ||
const getOffset = (index: number) => { | ||
'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.
step作为参数,改造为普通函数
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.
totalElements.value 直接访问children.length
const initStep: number = isNaN(pstep) ? 0 : pstep | ||
// 每个元素的宽度 or 高度,有固定值直接初始化无则0 | ||
const step = useSharedValue(initStep) | ||
const totalElements = useSharedValue(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.
childrenLength
// 用户点击未移动状态下,记录用户上一次操作的transtion 的 direction | ||
const customTrans = useSharedValue(0) | ||
const intervalTimer = props.interval || 500 | ||
totalElements.value = 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.
useEffect包裹
// 业务swiper-item自己生成key,内部添加的元素自定义key | ||
const newChild = React.cloneElement(child, { | ||
itemIndex: index, | ||
customStyle: [extraStyle] |
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.
不需要数组
// 按照长按的处理情况:1. 用户先快速滑动,再未滑动到目标位置时再次按下然后抬起;2. 用户在正常情况下点击没有触发移动 | ||
function handleLongPress (eventData: EventDataType) { | ||
'worklet' | ||
const { translation } = eventData |
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.
translation计算逻辑确认
}) | ||
.onTouchesUp((e) => { | ||
'worklet' | ||
const touchEventData = e.changedTouches[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.
if(veloctity<threshold){
handleEnd(currentIndex)
} else {
if(offsetDiff>0) handleEnd(currentIndex+1)
else handleEnd(currentIndex-1)
}
在move当中基于offset实时更新currentIndex
if (!props.circular && !canMove(eventData)) { | ||
return | ||
} | ||
if (!moveDistance) { |
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]) | ||
|
||
useEffect(() => { | ||
if (!step.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.
这里不需要return
No description provided.