React Native之觸摸事件(Touchable系列和onLongPress)

1 觸摸事件

普通點(diǎn)擊我們可以使用onPress方法,我們可以使用Touchable 系列控件設(shè)計(jì)我們的按鈕

TouchableHighlight 背景會(huì)在用戶手指按下時(shí)變暗

TouchableNativeFeedback用戶手指按下時(shí)形成類似墨水漣漪的視覺效果

TouchableOpacity指按下時(shí)降低按鈕的透明度,而不會(huì)改變背景的顏色

TouchableWithoutFeedback 不顯示任何視覺反饋

檢測(cè)用戶是否進(jìn)行了長按操作,可以在上面列出的任意組件中使用onLongPress屬性來實(shí)現(xiàn)


2 測(cè)試關(guān)鍵代碼如下

要記得導(dǎo)入相應(yīng)的組件

    import React, {Component} from 'react';
    import {Platform, ScrollView, StyleSheet, Text, View, TextInput, NativeModules, DeviceEventEmitter, Image, Button, AppRegistry, TouchableHighlight, TouchableOpacity, TouchableNativeFeedback, TouchableWithoutFeedback} from 'react-native';

    export default class App extends Component<Props> {
      
      render() {
        return (
          <View style={styles.container}>
            <View style={styles.buttonContainer}>
              <Button onPress={this.showMsg} title="press me"/>
            </View>  
            <View style={styles.buttonContainer}>
              <Button onPress={this.showMsg} title="press me" color="#841584"/>
            </View>
     
           <View style={styles.buttonContainer}>
              <TouchableHighlight onPress={this.showMsg} underlayColor="white">
                  <View style={styles.button}>
                      <Text style={styles.text}>TouchableHighlight</Text>
                  </View>
              </TouchableHighlight>
            </View>
     
            <View style={styles.buttonContainer}>
              <TouchableOpacity onPress={this.showMsg}>
                  <View style={styles.button}>
                      <Text style={styles.text}>TouchableOpacity</Text>
                  </View>
              </TouchableOpacity>
            </View>
     
            <View style={styles.buttonContainer}>
              <TouchableNativeFeedback onPress={this.showMsg}>
                  <View style={styles.button}>
                      <Text style={styles.text}>TouchableNativeFeedback</Text>
                  </View>
              </TouchableNativeFeedback>
            </View>
     
            <View style={styles.buttonContainer}>
              <TouchableWithoutFeedback onPress={this.showMsg}>
                  <View style={styles.button}>
                      <Text style={styles.text}>TouchableWithoutFeedback</Text>
                  </View>
              </TouchableWithoutFeedback>
            </View>
     
           <View style={styles.buttonContainer}>
              <TouchableWithoutFeedback onLongPress={this.showMsg}>
                  <View style={styles.button}>
                      <Text style={styles.text}>onLongPress me</Text>
                  </View>
              </TouchableWithoutFeedback>
            </View>
     
           <View style={styles.layoutButtonContainer}>
              <Button onPress={this.showMsg} title="onLongPress me"/>
          <Button onPress={this.showMsg} title="onLongPress me" color="#841584"/>
            </View>
          
     
          </View>
        );
      }
        //記得這里調(diào)用的時(shí)候不需要加上()
        showMsg(){
        alert("showMsg(){}");  
        }
        
        //記得末尾加上分號(hào),調(diào)用的時(shí)候也要加上()
        showMessage = () => {
            alert("showMessage = () => {}");
        };
    }
     
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center'
      },
      buttonContainer: {
        margin:10
      },
      layoutButtonContainer: {
        margin:10,
        flexDirection: 'row',
        justifyContent: 'space-between'
      },
      button: {
        alignItems: 'center',
        backgroundColor: '#842534'
      },
      text: {
        padding: 10,
        color: 'white'
      }
    });


 
3 手機(jī)效果如下

 

 


 










作者:chen.yu
深信服三年半工作經(jīng)驗(yàn),目前就職游戲廠商,希望能和大家交流和學(xué)習(xí),
微信公眾號(hào):編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進(jìn)階人工智能(鏈接)