[深入剖析React Native]React Native组件之Touchable*
Touchable*系列组件封装了触摸点击的相关事件,比如:触摸、点击、长按、反馈等,官方提供Touchable*系列组件包括四种:
TouchableWithoutFeedback官方建议不使用该组件,因为任何可以响应事件的组件在触摸or点击的时候应该有视觉上的反应效果,但是这个组件没有。这个组件看起来像Web效果而不是原生(Native)效果。
属性方法
该组件我们一般不会直接进行使用,下面三种Touchable*系列组件对于该组件的属性方法都可以进行使用。具体会具体演示这些属性方法的使用实例。 TouchableHighlight触摸高亮该组件进行封装视图触摸点击的属性。当手指点击按下的时候,该视图的不透明度会进行降低同时会看到相应的颜色(视图变暗或者变亮)。如果我们去查看该组件的源代码会发现,该底层实现是添加了一个新的视图。如果我们没有正确的使用,就可能不会出现正确的效果。例如:我们没有给该组件视图设置backgroudnColor的颜色值。
属性方法
TouchableWithoutFeedback、TouchableHighlight实例'use strict';
import React,{ Component } from 'react';
import {
StyleSheet,Text,View,TouchableHighlight,TouchableWithoutFeedback,} from 'react-native';
export default class TouchableMazouri extends Component {
render() {
return (
<View style={styles.container}>
<TouchableHighlight
underlayColor="rgb(210,230,255)"
activeOpacity={0.5}
style={styles.touchable}
onHideUnderlay={() => console.log("onHideUnderlay")}
onShowUnderlay={() => console.log("onShowUnderlay")}
// accessible={false}
onPress={() => console.log("onPress TouchableHighlight")}
onLongPress={() => console.log("onLongPress TouchableHighlight")}
onPressIn={() => console.log("onPressIn TouchableHighlight")}
onPressOut={() => console.log("onPressOut TouchableHighlight")}
pressRetentionOffset={{top:200,left:50,bottom:0,right:50}}
delayPressIn={2000}
delayPressOut={2000}
delayLongPress={2000}
>
<Text style={{fontSize:16}}>我是TouchableHighlight,点我吧</Text>
</TouchableHighlight>
<TouchableWithoutFeedback
style={styles.touchable}
// accessible={false}
onPress={() => console.log("onPress TouchableWithoutFeedback")}
onLongPress={() => console.log("onLongPress TouchableWithoutFeedback")}
onPressIn={() => console.log("onPressIn TouchableWithoutFeedback")}
onPressOut={() => console.log("onPressOut TouchableWithoutFeedback")}
>
<View><Text style={{fontSize:16}}>我是TouchableWithoutFeedback,点我吧</Text></View>
</TouchableWithoutFeedback>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},touchable: {
borderWidth: 1,borderRadius: 8,borderColor: '#e0e0e0',backgroundColor: '#e5eCee',}
});
TouchableOpacity透明度变化该组件封装了响应触摸事件。当点击按下的时候,该组件的透明度会降低。该组件使用过程中并不会改变视图的层级关系,而且我们可以非常容易的添加到应用并且不会产生额外的异常错误。 属性方法
TouchableNativeFeedback(Android专用)该封装了可以进行响应触摸事件的组件(仅限Android平台)。在Android平台上面该该组件可以使用原生平台的状态资源来显示触摸状态变化。【特别注意】现如今该组件只是支持仅有一个View的子视图实例(作为子节点)。在底层实现上面实际上面是创建一个新的RCTView的节点来进行替换当前的View节点视图,并且可以携带一些附加的属性。 该组件触摸反馈的背景图资源可以通过background属性进行自定义设置 'use strict';
import React,TouchableOpacity,TouchableNativeFeedback,right:50}}
delayPressIn={2000}
delayPressOut={2000}
delayLongPress={2000}
>
<Text style={{fontSize:16}}>我是TouchableHighlight,点我吧</Text>
</TouchableHighlight>
<TouchableWithoutFeedback
style={styles.touchable}
// accessible={false}
onPress={() => console.log("onPress TouchableWithoutFeedback")}
onLongPress={() => console.log("onLongPress TouchableWithoutFeedback")}
onPressIn={() => console.log("onPressIn TouchableWithoutFeedback")}
onPressOut={() => console.log("onPressOut TouchableWithoutFeedback")}
>
<View><Text style={{fontSize:16}}>我是TouchableWithoutFeedback,点我吧</Text></View>
</TouchableWithoutFeedback>
<TouchableOpacity
activeOpacity={0}
style={styles.touchable}
onPress={() => console.log("onPress TouchableOpacity")}
onLongPress={() => console.log("onLongPress TouchableOpacity")}
onPressIn={() => console.log("onPressIn TouchableOpacity")}
onPressOut={() => console.log("onPressOut TouchableOpacity")}
>
<Text style={{fontSize:16}}>我是TouchableOpacity,点我就隐藏</Text>
</TouchableOpacity>
<TouchableNativeFeedback
style={styles.touchable}
>
<View style={{backgroundColor: 'blue'}}>
<Text style={{fontSize:16}}>我是TouchableNativeFeedback,点我吧</Text>
</View>
</TouchableNativeFeedback>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,marginTop: 20,}
});
属性方法
生成该dictionary对象
'use strict';
import React,right:50}}
delayPressIn={2000}
delayPressOut={2000}
delayLongPress={2000}
>
<Text style={{fontSize:16}}>我是TouchableHighlight,点我吧</Text>
</TouchableHighlight>
<TouchableWithoutFeedback
style={styles.touchable}
// accessible={false}
onPress={() => console.log("onPress TouchableWithoutFeedback")}
onLongPress={() => console.log("onLongPress TouchableWithoutFeedback")}
onPressIn={() => console.log("onPressIn TouchableWithoutFeedback")}
onPressOut={() => console.log("onPressOut TouchableWithoutFeedback")}
>
<View><Text style={{fontSize:16}}>我是TouchableWithoutFeedback,点我吧</Text></View>
</TouchableWithoutFeedback>
<TouchableOpacity
activeOpacity={0}
style={styles.touchable}
onPress={() => console.log("onPress TouchableOpacity")}
onLongPress={() => console.log("onLongPress TouchableOpacity")}
onPressIn={() => console.log("onPressIn TouchableOpacity")}
onPressOut={() => console.log("onPressOut TouchableOpacity")}
>
<Text style={{fontSize:16}}>我是TouchableOpacity,点我就隐藏</Text>
</TouchableOpacity>
<TouchableNativeFeedback
style={styles.touchable}
>
<View style={{backgroundColor: 'blue'}}>
<Text style={{fontSize:16}}>我是TouchableNativeFeedback,点我吧</Text>
</View>
</TouchableNativeFeedback>
<TouchableNativeFeedback
style={styles.touchable}
background={TouchableNativeFeedback.SelectableBackground()}
>
<View>
<Text style={{fontSize:16}}>selectableItemBackground,点我吧</Text>
</View>
</TouchableNativeFeedback>
<TouchableNativeFeedback
style={styles.touchable}
background={TouchableNativeFeedback.SelectableBackgroundBorderless()}
>
<View>
<Text style={{fontSize:16}}>selectableItemBackgroundBorderless,点我吧</Text>
</View>
</TouchableNativeFeedback>
<TouchableNativeFeedback
style={styles.touchable}
background={TouchableNativeFeedback.Ripple('#fff000',false)}
>
<View>
<Text style={{fontSize:16}}>selectableItemBackgroundBorderless false,点我吧</Text>
</View>
</TouchableNativeFeedback>
<TouchableNativeFeedback
style={styles.touchable}
background={TouchableNativeFeedback.Ripple('#000fff',true)}
>
<View>
<Text style={{fontSize:16}}>selectableItemBackgroundBorderless true,点我吧</Text>
</View>
</TouchableNativeFeedback>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,}
});
转载请标注地址:Geek马走日 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |