1. 程式人生 > >【React Native開發】- 觸控事件處理

【React Native開發】- 觸控事件處理

1.介紹

React Native提供了可以處理觸控事件的元件。觸控即點選、長按、滑動、縮放。

2.點選

處理點選操作事件,可以使用Touchable類元件,通過此類元件的onPress屬性實現點選事件處理函式;一個完整的點選事件是

手指按下並擡起。

import React,{Component} from 'react'
import {
    StyleSheet,
    View,
    Text,
    TouchableHighlight,
    ToastAndroid,
} from 'react-native'
export default class Click extends 
Component{ constructor(props){ super(props) } render(){ return ( <View style={styles.container}> <TouchableHighlight activeOpacity ={0.8} underlayColor='#eee' onPress={this._click.bind(this)} style=
{styles.touchStyle}> <Text>點選事件</Text> </TouchableHighlight> </View> ) } _click(){ ToastAndroid.show('點選事件',1000) } } const styles = StyleSheet.create({ container:{ flex:1, justifyContent
:'center', alignItems:'center', }, touchStyle:{ padding:5, borderWidth:1, borderRadius:8, borderColor:'#ccc' }, })
效果:


3.長按

處理長按事件,可以使用Touchable類元件,通過此類元件的onLongPress屬性實現點選事件處理函式;

<TouchableHighlight
activeOpacity ={0.8}
   underlayColor='#eee'
onLongPress={this._longClick.bind(this)}
   style={styles.touchStyle}>
   <Text>長按事件</Text>
</TouchableHighlight>   

4.滑動

可滾動的列表是移動應用中一個常見的模式。

ScrollView可以在垂直或水平方向滾動,還可以配置pagingEnabled屬性來讓使用者整屏整屏的滑動。此外,水平方向的滑動還可以使用Android上的ViewPageAndroid元件。

ListView是一種特殊的ScrollView,用於顯示比較長的垂直列表。

5.縮放

在ScrollView中只放置一個元件,則可以用來實現縮放操作。設定maximumZoomScale(ios平臺獨有)minimumZoomScale(ios平臺獨有)屬性即可以使使用者能夠縮放其中的內容。