react native touchable highlight和touchable native feedback用于制作我的菜单项

yizd12fk  于 6个月前  发布在  React
关注(0)|答案(8)|浏览(93)

我试图实现一个基本的抽屉,但是我被文档搞糊涂了。
这里有一个可触摸的高亮显示,还有一个可触摸的本地反馈。
TouchableNativeFeedback仅适用于Android。那么,我如何才能最好地处理我的MenuItem组件,以便它同时处理Android和IOS?
这是我目前所得到的,我不知道如何处理不同平台的特定内容:

import React, { Component, PropTypes } from 'react';
import { TouchableHighlight, TouchableNativeFeedback, Platform, View, Text } from 'react-native';

export class MenuItem extends Component {

  handleAndroid() {

  }

  handleIOS() {

  }

  renderMenuItem() {
    return (
      <View style={styles.container}>
        <Text style={styles.text}>
          {this.props.text}
        </Text>
      </View>
    );
  }
  render() {
    return (

    );
  }
}

字符串
在Android上,我是否必须仅使用TouchableNativeFeedback?

4nkexdtk

4nkexdtk1#

就像你说的,TouchableNativeFeedback只适用于Android,因此它不适用于iOS。如果你想要一个适用于两者的解决方案,请使用TouchableHighlight并相应地设置它的样式。例如,它的underlayColor prop 允许你设置 “触摸激活时显示的颜色"

编辑:

如果你想在Android上使用TouchableNativeFeedback,在iOS上使用TouchableHighlight,你应该做以下事情:

import { Platform } from 'react-native'
...

render() {
    if (Platform.OS === 'android') {
       return (
          <TouchableNativeFeedback> 
               ... 
          </TouchableNativeFeedback>    
       )
    } else {
       return (
          <TouchableHighlight> 
               ... 
          </TouchableHighlight>    
       )
    }
}

字符串

编辑2:

或者,您可以为每个平台创建自定义组件并使用文件扩展名。例如:

MyButton.ios.js
MyButton.android.js


将这两个组件放在同一个文件夹中,然后使用MyButton作为常规组件:

import MyButton from '../path/to/component/MyButton'
...

render() {
   <MyButton/>
}


当你想在多个地方使用这个组件时,这是非常简洁的,因为你不用用if-else块来填充代码。
在这里,您可以阅读更多关于Platform Specific Code

3phpmpom

3phpmpom2#

你可以用一种更优雅的方式来解决这个问题,就像这样:

render() {
    let TouchablePlatformSpecific = Platform.OS === 'ios' ? 
        TouchableOpacity : 
        TouchableNativeFeedback;

    let touchableStyle = Platform.OS === 'ios' ? 
        styles.iosTouchable : 
        styles.androidTouchable

    return(
        <TouchablePlatformSpecific style={touchableStyle}>
            (...)
        </TouchablePlatformSpecific>   
    );
}

字符串
我一直在成功地使用它,我看不出它不起作用的原因,它对我来说很好。

mwngjboj

mwngjboj3#

我们可以有一个哑组件,它产生通用的Touchable,它的工作取决于平台和处理onPress事件。

可触摸组件:

import { Platform } from "react-native"

import { TouchableNativeFeedback, TouchableOpacity } from "react-native"
import React from "react"

export const Touchable = (props: any) => {
  return Platform.OS === 'android'
    ? <TouchableNativeFeedback onPress={props.onPress}>{props.children}</TouchableNativeFeedback>
    : <TouchableOpacity onPress={props.onPress}>{props.children}</TouchableOpacity>
}

字符串

用法:

import { Touchable } from '../path/to/touchable.component';
...
render() {
    <Touchable onPress={() => this.handleClick()}>
         .....
    </Touchable>
}


感谢@Giorgos的实现,它构成了这个答案的基础。

nzkunb0c

nzkunb0c4#

或者你可以创建一个像这样的自定义组件:

import { TouchableNativeFeedback, TouchableOpacity } from 'react-native'
...
const Touchable = (props) => {
  return Platform.OS === 'android'
    ? <TouchableNativeFeedback>{props.children}</TouchableNativeFeedback>
    : <TouchableOpacity>{props.children}</TouchableOpacity>
}

字符串
然后像这样使用它:

<Touchable>
    <Card>
       <Text>Click me!</Text>
    </Card>
</Touchable>

kxkpmulp

kxkpmulp5#

我也有几乎相同的需求,我最终使用了这个库react-native-haptic-feedback
请记住,触觉反馈仅在一些最新的Android设备和iPhone 6s以上的iOS中可用。对于没有触觉电机的设备,会有振动。下面是一个示例代码片段:

import ReactNativeHapticFeedback from "react-native-haptic-feedback";

const options = {
  enableVibrateFallback: true,
  ignoreAndroidSystemSettings: false
};

ReactNativeHapticFeedback.trigger("impactMedium", options);

字符串
在您的例子中,它将直接与按钮的onPress方法一起工作,例如:

<TouchableOpacity onPress={()=>{ReactNativeHapticFeedback.trigger("impactMedium", options);}} />


注意:我发现最多设备支持的最通用的类型是impactMedium

nwwlzxa7

nwwlzxa76#

基于@Nithish JV和Giorgos Karyofyllis:

import React from "react"
import { Platform } from "react-native"
import { TouchableNativeFeedback, TouchableOpacity } from "react-native"

export const TouchableFeedback = (props: any) => {
    const { children, ...rest } = props;
    return Platform.OS === 'android'
        ? <TouchableNativeFeedback {...rest}>{children}</TouchableNativeFeedback>
        : <TouchableOpacity {...rest}>{children}</TouchableOpacity>
}

字符串

bvhaajcl

bvhaajcl7#

最优雅的方式就是
第一个月
在渲染中:

TouchableButton = Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity
return (
    <TouchableButton>{'bla bla'}</TouchableButton>
)

字符串

3z6pesqy

3z6pesqy8#

由于操作系统在运行时不会神奇地改变,我总是使用这个小片段:

import { Component } from "react"
import {
    Platform,
    TouchableNativeFeedback,
    TouchableOpacity,
} from "react-native"

// The operating system doesn't magically change,
// so we can call the function directly to get the correct component
export default ((): typeof Component => {
    if (Platform.OS === "android") {
        return TouchableNativeFeedback
    }

    return TouchableOpacity // Choose whatever touchable you like
})()

字符串
它基本上是一个IIFE,为操作系统选择正确的触摸屏。

相关问题