一、简介

在应用程序中,最灵魂的功能就是交互。通过给应用程序的组件添加事件来实现交互,进而提高用户体验。然而,ReactNative并不能像Web开发那样可以给大多数的标签元素绑定click事件,例如div、button、input等等,但是RN除了可以通过Text的onPress完成事件外,还是额外提供了4个组件来解决这个问题。这4个组件统称为“Touchable类组件”,也即触摸类组件,使用它们就可以像Text组件那样通过onPress使得其他任意组件都可以被点击。分别是TouchableHighlight、TouchableOpacity、TouchableWithoutFeedback、TouchableNativeFeedback。(注意:TouchableNativeFeedback用于安卓,此处不做解释)

二、区别

这三个组件都能实现触摸点击,进而完成事件交互,但是产生的交互过渡效果是有区别的,具体如下:

三、详请

1、TouchableHighlight,它拥有明显视觉交互效果,这种效果能够很友善地告知用户当前点击事件被触发了,从而避免重复点击。它的主要属性和事件如下:

//触摸时透明度的设置
avtiveOpacity //隐藏背景阴影时触发该事件
onHideUnderlay //出现背景阴影时触发该事件
onShowUnderlay //点击时背景阴影效果的背景颜色
underlayColor

使用效果如下:点击时背景颜色发生改变,有比较明显的交互提示

/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/ import React, { Component } from 'react'; import {
AppRegistry,
StyleSheet,
View,
Text,
TouchableHighlight
} from 'react-native'; export default class ReactNativeDemo extends Component { render() {
return (
<View style={style.flex}>
<View style={style.touch}>
<TouchableHighlight
activeOpacity={0.5}
underlayColor={'#E1F6FF'}
onPress={() => alert("TouchableHighlight")}
onHideUnderlay={() => console.log("--onHideUnderlay--")}
onShowUnderlay={() => console.log("--onShowUnderlay--")}
>
<Text style={style.font}>Button</Text>
</TouchableHighlight>
</View>
</View>
);
}
} const style = StyleSheet.create({
flex: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
touch:{
borderColor:'blue',
borderWidth: 1,
borderRadius: 4,
justifyContent: 'center',
alignItems: 'center'
},
font:{
fontSize: 25,
fontWeight: 'bold',
color:'red'
}
}); AppRegistry.registerComponent('ReactNativeDemo', () => ReactNativeDemo);
-- ::02.747 [info][tid:com.facebook.react.JavaScript] --onShowUnderlay--
-- ::04.767 [info][tid:com.facebook.react.JavaScript] --onHideUnderlay--
-- ::04.768 [info][tid:com.facebook.react.JavaScript] --onShowUnderlay--
-- ::04.869 [info][tid:com.facebook.react.JavaScript] --onHideUnderlay-- 

2、TouchableOpacity,它也能给出交互提示,不过它不能设置背景色,只能设置通过透明度,反而更加方便。它仅有一个属性。

//触摸时透明度的设置
avtiveOpacity

使用效果如下:点击时透明度发生改变,也有较明显的交互提示

/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/ import React, { Component } from 'react'; import {
AppRegistry,
StyleSheet,
View,
Text,
TouchableOpacity
} from 'react-native'; export default class ReactNativeDemo extends Component { render() {
return (
<View style={style.flex}>
<View style={style.touch}>
<TouchableOpacity
activeOpacity={0.2}
onPress={() => alert("TouchableOpacity")}
>
<Text style={style.font}>Button</Text>
</TouchableOpacity>
</View>
</View>
);
}
} const style = StyleSheet.create({
flex: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
touch:{
borderColor:'blue',
borderWidth: 1,
borderRadius: 4,
justifyContent: 'center',
alignItems: 'center'
},
font:{
fontSize: 25,
fontWeight: 'bold',
color:'red'
}
}); AppRegistry.registerComponent('ReactNativeDemo', () => ReactNativeDemo);

3、TouchableWithoutFeedback,使用它绑定事件,不会有明显的交互提示,就跟Web交互一样,而不是Native交互,除非特殊情况,不推荐使用。它不能直接嵌套子组件Text组件,必须先隔一个父组件,否则自身事件属性不能触发,它支持三个自身属性事件如下:

//长按事件
onLongPress //触摸进入事件
onPressIn //触摸释放事件
onPressOut

使用效果如下:发现交互时无任何明显提示,长按打印日至

/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/ import React, { Component } from 'react'; import {
AppRegistry,
StyleSheet,
View,
Text,
TouchableWithoutFeedback
} from 'react-native'; export default class ReactNativeDemo extends Component { render() {
return (
<View style={style.flex}>
<View style={style.touch}>
<TouchableWithoutFeedback
onPress={() => alert("TouchableWithoutFeedback")}
onLongPress={() => console.log("--onLongPress--")}
onPressIn={() => console.log("--onPressIn--")}
onPressOut={() => console.log("--onPressOut--")}
>
<View><Text style={style.font}>Button</Text></View> /*Text嵌套在View组件中,然后在嵌套到该触摸类组件中*/
</TouchableWithoutFeedback>
</View>
</View>
);
}
} const style = StyleSheet.create({
flex: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
touch:{
borderColor:'blue',
borderWidth: 1,
borderRadius: 4,
justifyContent: 'center',
alignItems: 'center'
},
font:{
fontSize: 25,
fontWeight: 'bold',
color:'red'
}
}); AppRegistry.registerComponent('ReactNativeDemo', () => ReactNativeDemo);
-- ::48.796 [info][tid:com.facebook.react.JavaScript] --onPressIn--
-- ::49.301 [info][tid:com.facebook.react.JavaScript] --onLongPress--
-- ::50.465 [info][tid:com.facebook.react.JavaScript] --onPressOut--
-- ::52.096 [info][tid:com.facebook.react.JavaScript] --onPressIn--
-- ::52.100 [info][tid:com.facebook.react.JavaScript] --onPressOut--

ReactNative: 使用Touchable触摸类组件的更多相关文章

  1. cocos2d-x 源代码分析 : control 源代码分析 ( 控制类组件 controlButton)

    源代码版本号来自3.1rc 转载请注明 cocos2d-x源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承 ...

  2. ReactNative: 使用AppReistry注册类

    一.简介 每一个应用程序的运行都有一个入口文件或者入口函数,例如iOS中的使用UIApplicationMain类完成入口函数的实现,在React-Native中,AppRegistry类就肩负着这个 ...

  3. ReactNative Android之原生UI组件动态addView不显示问题解决

    ReactNative Android之原生UI组件动态addView不显示问题解决 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com ...

  4. 同学帮帮移动 H5 弹出层类组件:txbb-pop

    Txbb.Pop 同学帮帮弹出层类组件,简洁.无依赖,使用 CSS3 实现动画效果. 为什么要再造一遍轮子 弹出层是常见的业务场景,而且弹出层的业务场景很简单,没必要使用大而全的库,并且,我们经常会有 ...

  5. React - 组件:类组件

    目录: 1. 类组件有自己的状态 2. 继承React.Component-会有生命周期和this 3. 内部需要一个render函数(类组件会默认调用render方法,但不会默认添加,需要手动填写r ...

  6. vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式

    vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式 class类组件示例 Father类组件 <template> & ...

  7. DRF-视图类组件

    补充 GET  books-------->查看数据--------------------> 返回所有数据列表 :[{},{},{}] POST books-------->添加数 ...

  8. 类组件(Class component)和函数式组件(Functional component)之间有何不同

    类组件不仅允许你使用更多额外的功能,如组件自身的状态和生命周期钩子,也能使组件直接访问 store 并维持状态当组件仅是接收 props,并将组件自身渲染到页面时,该组件就是一个 ‘无状态组件(sta ...

  9. 你真的会用Flutter日期类组件吗

    Flutter系统提供了一些日期选择类组件,比如DayPicker.MonthPicker.YearPicker.showDatePicker.CupertinoDatePicker等,其中前4个为M ...

随机推荐

  1. 【Web技术】281- 滴滴开源小程序框架 Mpx2.0

    滴滴Mpx框架负责人@hiyuki,滴滴出行网约车webapp乘客团队的负责人,也是滴滴开源的小程序框架Mpx的负责人和核心作者 Mpx是一款致力于提高小程序开发体验和效率的增强型小程序框架,目前在滴 ...

  2. postman设置全局变量

    //处理token var jsn = JSON.parse(responseBody) console.log(jsn.access_token) //把access_token设置到全局变量中 p ...

  3. eclipse新建maven项目报错Could not resolve arachetype org.apache.maven.archetypes:mmaven-archetype-quickstart:1.1 from any of the configured repositories

    使用eclipse新建maven项目,按下图所示选择后,报错 报错截图 报错详细信息 Could not resolve archetype org.apache.maven.archetypes:m ...

  4. vue+element 中 el-input框 限制只能输入数字及一位小数

    仅个人经验,希望能帮到有需要的人. 第一次写 就话不多说了直接上代码. <el-input @keyup.native="proving(index)" v-model=&q ...

  5. android 7.0+、8.0+应用中点击拍照崩溃的解决办法

    在开发中,项目里面明明已经添加过拍照或者读取相册的权限,但是在点击拍照或者打开相册的时候应用会崩溃,报一下错误: Caused by: android.os.FileUriExposedExcepti ...

  6. Tomcat系列(一)- 整体架构

    整体架构 我们想要了解一个框架,首先要了解它是干什么的,Tomcat我们都知道,是用于处理连接过来的Socket请求的.那么Tomcat就会有两个功能: 对外处理连接,将收到的字节流转化为自己想要的R ...

  7. Github挂载大文件解决方案

    正常情况下,我们上传代码之类的文本文件,都不会太大,可以直接通过[Upload Files]选项直接上传. 但是这样的操作仅限文件大小在25MB以内. 如果你选择的文件超过25MB,那么Github会 ...

  8. 【CV现状-3.1】图像分割

    #磨染的初心--计算机视觉的现状 [这一系列文章是关于计算机视觉的反思,希望能引起一些人的共鸣.可以随意传播,随意喷.所涉及的内容过多,将按如下内容划分章节.已经完成的会逐渐加上链接.] 缘起 三维感 ...

  9. 如何在阿里云服务器上搭建wordpress个人网站

    1.购买云服务器.域名.域名解析.配置linux系统上的web环境.FTP等参照下面的链接. https://www.cnblogs.com/smyhvae/p/4965163.html?tdsour ...

  10. Java面试题_第二阶段(Servlet、HTTP、Session、JSP、 Ajax、Filter、JDBC、Mysql、Spring)

    1.1. 描述Servlet调用过程? 答案: (1)在浏览器输入地址,浏览器先去查找hosts文件,将主机名翻译为ip地址,如果找不到就再去查询dns服务器将主机名翻译成ip地址. (2)浏览器根据 ...