react-native 實現(xiàn)類似swiper 曲面滑動效果

需求:

實現(xiàn)一個類似于swiper滑動的效果。不過沒有用swiper實現(xiàn)。是自己寫了個函數(shù)去控制了;直接基于swiper實現(xiàn)應(yīng)該會更簡單-等我后面再試試的吧~

這是之前寫的一個小功能。當(dāng)初好像寫的好像有點費勁-今天分享下- 有相同需求的小伙伴可以參考下思路~
代碼可以直接復(fù)制 運行查看效果

123123.gif

上代碼

import React, {Component} from "react";
import {Image, LayoutAnimation, NativeModules, Platform, StyleSheet, Text, View} from "react-native";
const {UIManager} = NativeModules;
const whiteImage = 'https://img13.ihomefnt.com/arts-centre/UserArt/1b606941456e500aeaef2e6e5cdad4b55a42e6f5e6ad9101aee9ebe1c94a0f5c.png!original';
const imageList = [{
    picUrl: "https://img9.ihomefnt.com/30f4dfce7e781917bb1261d2f04b845cebbd0f6e924df7d1d1bb549f05c1b7d5.jpg!H-MIDDLE",
    index: 0
},
    {
        picUrl: "https://img9.ihomefnt.com/0e080f963528294137fd793b3698dc0be072f087ca0aa9894d98e34398d6dbd9.jpg!H-MIDDLE",
        index: 1
    },
    {
        picUrl: "https://img9.ihomefnt.com/b1f5304a99374d505f69238ac5f0262a6747fb91a09e35ec08790b2df6657a19.jpg!H-MIDDLE",
        index: 2
    },
    {
        picUrl: "https://img9.ihomefnt.com/5beda375dbb22a112c025b39fcfa714fed9133a7c42eb9003ef02fdea9f6c7a0.jpg!H-MIDDLE",
        index: 3
    },
    {
        picUrl: "https://img9.ihomefnt.com/30f4dfce7e781917bb1261d2f04b845cebbd0f6e924df7d1d1bb549f05c1b7d5.jpg!H-MIDDLE",
        index: 4
    },
    {
        picUrl: "https://img9.ihomefnt.com/9024f5d66a650c2d09220a2cdcc703225d143c39acc843930e7eac472b2cdb0f.jpg!H-MIDDLE",
        index: 5
    },
    {
        picUrl: "https://img9.ihomefnt.com/a09f7b7c939462c48bcab38d4d2921d24e1b858cb614af1ce6a03cde8befc972.jpg!H-MIDDLE",
        index: 6
    },
]

export default class Sliding extends Component {
    state = {
        changeIndex: 0, //當(dāng)前中心位置是第幾個
        showImgList: imageList //圖片的信息

    }
    startX: any; //記錄開始的位置
    _onPress = (type: string) => {
        let {showImgList, changeIndex} = this.state;
        LayoutAnimation.easeInEaseOut();
        //如果第是6個的話 就禁止滑動
        if ((type === "add" && (changeIndex === 6 || changeIndex + 1 === showImgList.length)) || (changeIndex === 0 && type === "less") || showImgList.length === 1) {
            return false;
        }
        showImgList.map((item: any) => {
            if (type === "add") {
                item.index -= 1;
            } else {
                item.index += 1;
            }
            return item;
        });
        //給數(shù)組里面下標(biāo)更新數(shù)字
        this.setState({
            changeIndex: (type === "add" ? changeIndex + 1 : changeIndex - 1),
            showImgList,
        })
    };


    componentWillUpdate() {
        LayoutAnimation.easeInEaseOut();
        //安卓端動畫支持
        if (Platform.OS === 'android') {
            UIManager.setLayoutAnimationEnabledExperimental(true)
        }
    }
    //結(jié)束后
    onTouchEnd = (e: any) => {
        let endX = e.nativeEvent.pageX;
        if (Math.abs(endX - this.startX) > 30) {
            //判斷是向左還是向右
            if (endX > this.startX) {
                this._onPress("less");
            } else {
                this._onPress("add");
            }
        }
    }

    //點擊記錄 按下位置
    onTouchStart = (e: any) => {
        this.startX = e.nativeEvent.pageX;
    }

    render() {
        let {showImgList, changeIndex} = this.state;
        const style = [
            styles.location0,
            styles.location1,
            styles.location2,
            styles.location3,
            styles.location4
        ];
        let picUrl = showImgList[changeIndex] && showImgList[changeIndex].picUrl;
        return (
            <View style={{flex: 1}}>
                <View style={styles.goodsImgView}>
                    <Image source={{uri: picUrl}} style={styles.goodsBigImg}/>
                </View>
                <View style={styles.changeView} onTouchStart={this.onTouchStart} onTouchEnd={this.onTouchEnd}>
                    <View style={styles.whiteView}>
                        {/* 切換的模塊了 */}
                        {showImgList.map((item: any, index: number) => {
                            return (
                                <Image key={index}
                                       source={{uri: item.picUrl}}
                                       style={[style[item.index + 2]]}/>
                            )
                        })}
                        <Image source={{uri: whiteImage}} style={styles.whiteBac}/>
                        {/* 基本操作 */}
                        <View style={styles.circle}/>
                        <Text
                            style={styles.toggleTitle}
                        >
                            左右滑動切換圖案
                        </Text>
                    </View>
                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    goodsImgView: {
        width: 375,
        flex: 3,
        justifyContent: 'center',
        alignItems: 'center',
    },
    goodsBigImg: {
        width: 270,
        height: 325,
    },
    changeView: {
        width: 375,
        flex: 2,
        justifyContent: "flex-end",
        alignItems: "center"
    },
    whiteView: {
        width: 375,
        height: 135,
        alignItems: "center"
    },
    whiteBac: {
        width: 375,
        height: 135,
        position: "absolute",
        zIndex: 12
    },
    toggleTitle: {
        paddingTop: 45,
        color: "#8D8B8A",
        fontSize: 14,
        zIndex: 12,
        position: "absolute",
        bottom: 65,
    },

    circle: {
        backgroundColor: "#ED7100",
        width: 10,
        height: 10,
        borderRadius: 5,
        position: "absolute",
        top: 30,
        zIndex: 13
    },
    location0: {
        position: "absolute",
        bottom: 70,
        left: 20,
        width: 90,
        height: 90,
        transform: [{rotate: "-35deg"}],
        zIndex: 9,
        borderRadius: 6
    },
    location1: {
        position: "absolute",
        bottom: 94,
        left: 55,
        width: 105,
        height: 105,
        transform: [{rotate: "-17deg"}],
        zIndex: 10,
        borderRadius: 6
    },
    location2: {
        position: "absolute",
        bottom: 100,
        width: 130,
        height: 130,
        zIndex: 11,
        borderRadius: 6
    },
    location3: {
        position: "absolute",
        bottom: 94,
        right: 55,
        width: 105,
        height: 105,
        transform: [{rotate: "17deg"}],
        zIndex: 10,
        borderRadius: 6
    },
    location4: {
        position: "absolute",
        bottom: 70,
        right: 20,
        width: 90,
        height: 90,

        transform: [{rotate: "35deg"}],
        zIndex: 9,
        borderRadius: 6
    }
});

代碼可以直接復(fù)制到RN里面進行運行- 實現(xiàn)的邏輯也在代碼中注釋了-下面說一下思路吧

TIPS:簡單的說一下思路

由于我這個需求是下方固定好6個方塊。第一個不可以向左滑動。第6個不可以向右滑動-其他人有相似需要 進行修改即可;

  • 首先寫好6個滑動塊的樣式-這邊用的是絕對定位和層級 旋轉(zhuǎn)好不同角度
  • 在最外層的VIew 綁定 按下(onTouchStart) 和松開事件(onTouchEnd)
  • 按下事件記錄下當(dāng)前按的X軸位置,在松后時候獲取到離開的X軸位置。進行判斷是向左向右
  • 得到結(jié)果后。對現(xiàn)有數(shù)組進行更新。更新數(shù)組中index的下標(biāo)
  • 在渲染時候根據(jù)下標(biāo)更換不同的位置style={[style[item.index + 2]]}
  • 中間加入LayoutAnimation.easeInEaseOut()動畫效果 即可;

當(dāng)然了 也可以根據(jù)reactNative的手勢進行判斷,回頭自己在基于swiper看是否可以實現(xiàn)
此文希望幫助到有需要的小伙伴們。有好的實現(xiàn)方案 也可以留言。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容