美文网首页
RN之处理触摸学习

RN之处理触摸学习

作者: 一只西西狸 | 来源:发表于2019-10-11 16:01 被阅读0次

移动应用上的用户交互基本靠“摸”。当然,“摸”也是有各种姿势的:在一个按钮上点击,在一个列表上滑动,或是在一个地图上缩放。React Native提供了可以处理常见触摸手势(例如点击或滑动)的组件,以及可用于识别更复杂的手势的完整的手势响应系统

显示一个简单的按钮

Button是一个简单的跨平台的按钮组件。下面是一个最简单的示例:

<Button
  onPress={()=>{
      Alert.alert("你点击了按钮!");
}}
title="点我!"
/>

上面这段代码会在iOS上渲染一个蓝色的标签状按钮,在Android上则会渲染一个蓝色圆角矩形带白字的按钮。点击这个按钮会调用”onPress“函数,具体作用就是显示一个alert弹出框。你还可以指定”color“属性来修改按钮的颜色。



再试试下面这个使用Button的例子吧。你可以点击”Tap to Play“来预览真实效果。(下面会显示一个嵌在网页中的手机模拟器,国内用户可能打不开这个网页模拟器,或速度非常慢)。

import React, { Component } from 'react';
import { Alert, Button, StyleSheet, View } from 'react-native';

export default class ButtonBasics extends Component {
  _onPressButton() {
    Alert.alert('You tapped the button!')
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={styles.buttonContainer}>
          <Button
            onPress={this._onPressButton}
            title="Press Me"
          />
        </View>
        <View style={styles.buttonContainer}>
          <Button
            onPress={this._onPressButton}
            title="Press Me"
            color="#841584"
          />
        </View>
        <View style={styles.alternativeLayoutButtonContainer}>
          <Button
            onPress={this._onPressButton}
            title="This looks great!"
          />
          <Button
            onPress={this._onPressButton}
            title="OK!"
            color="#841584"
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
   flex: 1,
   justifyContent: 'center',
  },
  buttonContainer: {
    margin: 20
  },
  alternativeLayoutButtonContainer: {
    margin: 20,
    flexDirection: 'row',
    justifyContent: 'space-between'
  }
})

运行结果如图



Touchable系列组件

这个组件的样式是固定的,所以如果它的外观并不怎么搭配你的设计,那就需要使用TouchableOpacity或是TouchableNativeFeedback组件来定制自己所需要的按钮,你可以在github.com网站上搜索’react native button‘来看看社区其他人的作品。
具体使用哪种组件,取决于你希望给用户什么样的视觉反馈:

  • 一般来说,你可以使用TouchableHighlight来制作按钮或者连接。注意此组件的背景会在用户手指按下时变暗。
  • 在Android上还可以使用TouchableNativeFeedback,它会在用户手指按下时行程类似墨水涟漪的视觉效果。
  • TouchableOpacity会在用户手指按下时降低按钮的透明度,而不会改变背景的颜色。
  • 如果你想在处理点击事件的同时不显示任何视觉反馈,则需要使用TouchableWithoutFeedback。

某些场景中你可能需要检测用户是否进行了长按操作。可以在上面列出的人已组建中使用onLongPress属性来实现。
我们来看一下示例:

import React, { Component } from 'react';
import { Alert, Platform, StyleSheet, Text, TouchableHighlight, TouchableOpacity, TouchableNativeFeedback, TouchableWithoutFeedback, View } from 'react-native';

export default class Touchables extends Component {
  _onPressButton() {
    Alert.alert('You tapped the button!')
  }

  _onLongPressButton() {
    Alert.alert('You long-pressed the button!')
  }


  render() {
    return (
      <View style={styles.container}>
        <TouchableHighlight onPress={this._onPressButton} underlayColor="white">
          <View style={styles.button}>
            <Text style={styles.buttonText}>TouchableHighlight</Text>
          </View>
        </TouchableHighlight>
        <TouchableOpacity onPress={this._onPressButton}>
          <View style={styles.button}>
            <Text style={styles.buttonText}>TouchableOpacity</Text>
          </View>
        </TouchableOpacity>
        <TouchableNativeFeedback
            onPress={this._onPressButton}
            background={Platform.OS === 'android' ? TouchableNativeFeedback.SelectableBackground() : ''}>
          <View style={styles.button}>
            <Text style={styles.buttonText}>TouchableNativeFeedback</Text>
          </View>
        </TouchableNativeFeedback>
        <TouchableWithoutFeedback
            onPress={this._onPressButton}
            >
          <View style={styles.button}>
            <Text style={styles.buttonText}>TouchableWithoutFeedback</Text>
          </View>
        </TouchableWithoutFeedback>
        <TouchableHighlight onPress={this._onPressButton} onLongPress={this._onLongPressButton} underlayColor="white">
          <View style={styles.button}>
            <Text style={styles.buttonText}>Touchable with Long Press</Text>
          </View>
        </TouchableHighlight>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    paddingTop: 60,
    alignItems: 'center'
  },
  button: {
    marginBottom: 30,
    width: 260,
    alignItems: 'center',
    backgroundColor: '#2196F3'
  },
  buttonText: {
    textAlign: 'center',
    padding: 20,
    color: 'white'
  }
})

运行结果如图



截图看不出效果,点击各个按钮时会出现不同的点击效果


另一个在移动应用中常见的手势就是滑动。用户会在列表中上下滑动,或是在视图上左右滑动。要处理这样的手势,下面一章学习的就是如何使用滚动视图。

相关文章

  • RN之处理触摸学习

    移动应用上的用户交互基本靠“摸”。当然,“摸”也是有各种姿势的:在一个按钮上点击,在一个列表上滑动,或是在一个地图...

  • RN记录学习

    1.RN和WebVIew通信2.模拟数据3.RN触摸事件处理4.Android打包5.Redux 的connect...

  • ReactNative中的手势冲突

    本文转自:这里 前言: 相对于原生的触摸事件处理机制,RN也有一套自己的处理机制,大体上和原生差不多,但是基于RN...

  • RN触摸详解

    连接: RN触摸详解

  • React-Native 手势处理详解1

    本文尝试介绍 RN 中触摸事件处理。 前言: react-native封装了一系列的组件例如

  • iOS学习笔记06-手势识别

    一、UIGestureRecognizer简单介绍 我们已经学习了触摸事件处理,但触摸事件处理起来很麻烦,每个触摸...

  • ReactNative touch事件

    1.RN的基本触摸事件 RN的组件除了TextInput、ScrollView(ListView)外,默认是不支持...

  • RN打造自己的大图查看浏览

    rn触摸手势学习——PanResponder。打造一个大图浏览功能,实现:单击事件、双击事件(双击缩放图片)、长按...

  • 开启 RN 学习之旅

    开启RN学习之旅 (一) —— 基本了解开启RN学习之旅 (二) —— RN - GitHub Project

  • reactnative cookie处理

    react native cookie处理 1. 工程环境 Native-android + rn :原生和rn混...

网友评论

      本文标题:RN之处理触摸学习

      本文链接:https://www.haomeiwen.com/subject/opwtmctx.html