【问题标题】:Show previous card on left to right swipe in react native在本机反应中从左到右滑动显示上一张卡片
【发布时间】:2018-08-12 07:57:38
【问题描述】:

我对本机反应很陌生。我想在我的应用程序中实现刷卡,因为我使用了react-native-tinder-swipe-cards

是否可以在从左向右滑动时显示上一张卡片。即使我减少 index 的值,下一张卡片也会显示在数组的下一个索引处。

我现在正在尝试的是:

 handleYup(card) {
    console.log("yup",card)
    //Alert.alert("Card",card.id+"");
    this.setState({
        cardYup: true
    })

}

handleNope(card) {
    console.log("nope")
    //Alert.alert("Card", card.id + "");
    this.setState({
        cardNope: true
    })

}

cardRemoved(index) {
    if(this.state.cardYup){
        index=index-2;
        this.setState({
            cardYup:false
        })
    }
    else if (this.state.cardNope) {
        //index = index +1
        this.setState({
            cardNope: false
        })
    }
    console.log(`The index is ${index}`);
    Alert.alert("Index", index+"");

    let CARD_REFRESH_LIMIT = 1

    if (this.state.cards.length - index <= CARD_REFRESH_LIMIT) {
        console.log(`There are only ${this.state.cards.length - index - 1} cards left.`);

        if (!this.state.outOfCards) {
            //console.log(`Adding ${cards2.length} more cards`)
            Alert.alert("No Cards","No morecards present")

            this.setState({
                outOfCards: true
            })
        }

    }

}

【问题讨论】:

  • 我不认为图书馆对此提供支持。但是您可以在该库中拥有自己的逻辑。它有一个名为 _goToPrevCard 的方法,但它没有在任何地方使用,因此您可能需要在库本身中实现它,而不是从代码中调用它。

标签: javascript react-native


【解决方案1】:

为了确保可以在现有库中添加一些功能,但由于时间限制,我将库从 react-native-tinder-swipe-cards. 切换到 react-native-deck-swiper

在应用所有这些道具后,我能够获得所需的功能

            <Swiper
                ref={swiper => {
                    this.swiper = swiper
                }}
                goBackToPreviousCardOnSwipeRight={true}
                showSecondCard={false}
                onSwiped={this.onSwiped}
                onTapCard={this.swipeLeft}
                cards={this.state.cards}
                cardIndex={id}
                renderCard={this.renderCard}
                verticalSwipe={false}
                cardVerticalMargin={5}
                cardHorizontalMargin={5}
                onSwipedAll={this.onSwipedAllCards}
                cardStyle={{height:520}}>
            </Swiper>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2019-07-13
    相关资源
    最近更新 更多