使用Animated.View和PanResponder寫了個半成品的slide組建

純tsx復(fù)制即用

import {Animated, PanResponder, StyleSheet, View} from "react-native";
import * as React from "react";
import {useRef, useState} from "react";


interface NJSlideViewProps {
    iWidth: number,
    iHeight: number | 15,
    value: number | 0.5,
    progress: (progress: number, scroll: boolean) => void
    color?: Array<string> | ["white", "red"],
}

export function NJSlideView(props: NJSlideViewProps) {
    let color = ["white", "red"]
    let value=0.5
    if (props.color !== undefined) {
        color = props.color
    }
    if (props.value!==undefined){
        value=props.value>1?1:props.value
    }
    const [parentX, setParentX] = useState(0)
    const pan = useRef(new Animated.ValueXY({x: (props.iWidth - props.iHeight) * value+props.iHeight/2, y: 0})).current;
    const panResponder = PanResponder.create({
        onStartShouldSetPanResponder: () => true,
        onMoveShouldSetPanResponderCapture: () => true,
        onPanResponderGrant: (e, g) => {
            // console.log(e.nativeEvent)
            // console.log(e.nativeEvent.pageX-parentX)
            pan.setOffset({
                x: e.nativeEvent.pageX - parentX ,
                y: 0
            })
        },
        onPanResponderMove: (event, g) => {
            //滑動計算 偏移props.iHeight/2個單位
            let dx = parentX + props.iHeight/2 < g.x0 + g.dx
                ? g.x0 + g.dx < parentX + props.iWidth-props.iHeight/2
                    ? g.dx
                    : parentX + props.iWidth- props.iHeight/2 - g.x0
                : parentX + props.iHeight/2 - g.x0
                pan.setValue({
                    x: dx,
                    y: 0
                })
        },
        onPanResponderRelease: (event,g) => {
            pan.flattenOffset()
        },
    });

    return (
        <View
            onLayout={event => {
                console.log(event.nativeEvent)
                setParentX(event.nativeEvent.layout.x)
            }}
            style={[NJSlideViewStyle.container, {width: props.iWidth,height:props.iHeight}]}>
            <View
                onTouchStart={e => {
                    let progress = e.nativeEvent.locationX / props.iWidth
                    pan.setValue({
                        x: (props.iWidth - props.iHeight) * progress+props.iHeight/2,
                        y: 0
                    })
                    pan.flattenOffset()
                    props.progress(progress, false)
                }}
                style={{width: props.iWidth, height: props.iHeight, position: "absolute", justifyContent: "center"}}>
                <View style={{
                    width: props.iWidth,
                    height: props.iHeight / 3,
                    borderRadius: props.iHeight / 3,
                    backgroundColor: color[0]
                }}>
                    <Animated.View style={{
                        width: pan.x,
                        height: props.iHeight / 3,
                        borderRadius: props.iHeight / 3,
                        backgroundColor: color[1]
                    }}/>
                </View>
            </View>
            <Animated.View
                {...panResponder.panHandlers}
                style={
                    [NJSlideViewStyle.seekbar,
                        {
                            height: props.iHeight,
                            width: props.iHeight,
                            marginLeft: -props.iHeight/2,
                            borderRadius:props.iHeight,
                            backgroundColor: color[1]
                        },
                        pan.getLayout()]}>
            </Animated.View>
        </View>
    )
}

const NJSlideViewStyle = StyleSheet.create({
    container: {
        height: 30,
        alignItems: "flex-start",
        justifyContent: "center",
    },
    seekbar: {
        width: 30,
        height: 30,
        borderRadius: 20,
    }
})


使用方法

                <NJSlideView
                  value={0.01}
                    iWidth={width-80}
                    iHeight={30}
                    color={["white","red"]}
                    progress={(progress, scroll) => {
                        console.log(progress)
                    }}/>
                <NJSlideView
                    value={0.99}
                    iWidth={width-80}
                    iHeight={10}
                    color={["white","#0cee12"]}
                    progress={(progress, scroll) => {
                        console.log(progress)
                    }}/>

效果

Simulator Screen Shot - iPhone 13 - 2021-12-10 at 14.51.11.png

目前的bug是當(dāng)組件被一個沒有具體寬帶的View 包裹時四啰,拖拽進(jìn)度條按鈕的偏移量會計算出錯径荔。受前面bug的影響拖拽實時的進(jìn)度我沒計算少辣,拖拽完成的進(jìn)度我也沒計算耐齐。

我剛開始的寫的這個組件是希望能實現(xiàn)年齡段的選擇厂抽,(就是能在一條線上需频,選出最大年齡,和最小年齡的功能)筷凤。不知道把頭發(fā)干禿能不能做出來昭殉。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市藐守,隨后出現(xiàn)的幾起案子挪丢,更是在濱河造成了極大的恐慌,老刑警劉巖卢厂,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乾蓬,死亡現(xiàn)場離奇詭異,居然都是意外死亡慎恒,警方通過查閱死者的電腦和手機(jī)任内,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門撵渡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人死嗦,你說我怎么就攤上這事趋距。” “怎么了越除?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵节腐,是天一觀的道長。 經(jīng)常有香客問我摘盆,道長翼雀,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任骡澈,我火速辦了婚禮锅纺,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘肋殴。我一直安慰自己,他們只是感情好坦弟,可當(dāng)我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布护锤。 她就那樣靜靜地躺著,像睡著了一般酿傍。 火紅的嫁衣襯著肌膚如雪烙懦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天赤炒,我揣著相機(jī)與錄音氯析,去河邊找鬼。 笑死莺褒,一個胖子當(dāng)著我的面吹牛掩缓,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播遵岩,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼你辣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了尘执?” 一聲冷哼從身側(cè)響起舍哄,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎誊锭,沒想到半個月后表悬,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡丧靡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年蟆沫,在試婚紗的時候發(fā)現(xiàn)自己被綠了籽暇。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡饥追,死狀恐怖图仓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情但绕,我是刑警寧澤救崔,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站捏顺,受9級特大地震影響六孵,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜幅骄,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一劫窒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拆座,春花似錦主巍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至躏碳,卻和暖如春搞旭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背菇绵。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工肄渗, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人咬最。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓翎嫡,卻偏偏與公主長得像,于是被迫代替她去往敵國和親丹诀。 傳聞我的和親對象是個殘疾皇子钝的,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容