FlingGestureHandler - react-nati

2019-04-03  本文已影响0人  JamesSawyer

文档来源:

分离类型handler, 当动作足够长并且快时激活。当handler被激活,手指释放是,state将变为 END

如果handler在激活之前,手指就松开了,handler将识别失败,iOS中使用 UISwipeGestureRecognizer 实现,而Android中是自定义实现。

属性 (properties)

属性除了公用属性外,下面是 FlingGestureHandler 的特定属性:

事件数据(event data)

除了基本的event属性,下面是 FlingGestureHandler特定的event属性:


示例 滑动

/**
 * 当使用2个手指向上滑动时,小球会向下移动50points
 * 当使用1根手指向左或者向右滑动时,小球会向左移动 50poings
 */
import React, { PureComponent } from 'react';
import { Animated, StyleSheet, View, Dimensions, Text, SafeAreaView } from 'react-native';
import { FlingGestureHandler, State, Directions } from 'react-native-gesture-handler';

import { USE_NATIVE_DRIVER } from '../config';

const { width: windowWidth } = Dimensions.get('window');
const circleRadius = 40;

const styles = StyleSheet.create({
  horizontalPan: {
    backgroundColor: '#f76f41',
    height: 300,
    justifyContent: 'center',
    marginVertical: 10,
  },
  circle: {
    backgroundColor: '#42a5f5',
    borderRadius: circleRadius,
    height: circleRadius * 2,
    width: circleRadius * 2,
  }
})

class Fling extends PureComponent {
  constructor(props) {
    super(props);
    this._touchX = new Animated.Value(windowWidth / 2);
    this._translateX = Animated.add(
      this._touchX,
      new Animated.Value(-circleRadius)
    );
    this._translateY = new Animated.Value(0);
  }

  // 水平方向滑动
  _onHorizontalFlingHandlerStateChange = ({ nativeEvent }, offset) => {
    if (nativeEvent.oldState === State.ACTIVE) {
      Animated.spring(this._touchX, {
        toValue: this._touchX._value + offset,
        useNativeDriver: USE_NATIVE_DRIVER,
      }).start();
    }
  };

  // 垂直方向滑动
  _onVerticalFlingHandlerStateChange = ({ nativeEvent }) => {
    if (nativeEvent.oldState === State.ACTIVE) {
      Animated.spring(this._translateY, {
        toValue: this._translateY._value + 50,
        useNativeDriver: USE_NATIVE_DRIVER,
      }).start();
    }
  };

  render() {
    return (
      <FlingGestureHandler
        direction={Directions.UP}
        numberOfPointers={2}
        onHandlerStateChange={this._onVerticalFlingHandlerStateChange}>
        <FlingGestureHandler
          direction={Directions.RIGHT | Directions.LEFT}
          onHandlerStateChange={ev =>
            this._onHorizontalFlingHandlerStateChange(ev, -50)
          }>
          <View style={styles.horizontalPan}>
            <Animated.View
              style={[
                styles.circle,
                {
                  transform: [
                    {
                      translateX: this._translateX,
                    },
                    {
                      translateY: this._translateY,
                    },
                  ],
                },
              ]}
            />
          </View>
        </FlingGestureHandler>
      </FlingGestureHandler>
    );
  }
}



export default class FlingGestureExample extends PureComponent {
  render() {
    return (
      <SafeAreaView>
        <Fling />
        <Text>
          向上使用2根手指移动或者使用一根手指向左或右移动,看会发生什么
        </Text>
      </SafeAreaView>
    )
  }
}

个人感觉这个示例没啥子用,主要知识点:

上一篇 下一篇

猜你喜欢

热点阅读