|
| 1 | +<!-- {% raw %} --> |
| 2 | +> 模板版本:v0.2.2 |
| 3 | +
|
| 4 | +<p align="center"> |
| 5 | + <h1 align="center"> <code>react-native-shake</code> </h1> |
| 6 | +</p> |
| 7 | +<p align="center"> |
| 8 | + <a href="https://github.com/Doko-Demo-Doa/react-native-shake"> |
| 9 | + <img src="https://img.shields.io/badge/platforms-android%20|%20ios%20|%20harmony%20-lightgrey.svg" alt="Supported platforms" /> |
| 10 | + </a> |
| 11 | + <a href="https://github.com/Doko-Demo-Doa/react-native-shake/blob/main/LICENSE"> |
| 12 | + <img src="https://img.shields.io/badge/license-MIT-green.svg" alt="License" /> |
| 13 | + </a> |
| 14 | +</p> |
| 15 | + |
| 16 | +> [!TIP] [Github 地址](https://github.com/react-native-oh-library/react-native-shake) |
| 17 | +
|
| 18 | +## 安装与使用 |
| 19 | + |
| 20 | +请到三方库的 Releases 发布地址查看配套的版本信息:[@react-native-oh-tpl/react-native-shake Releases](https://github.com/react-native-oh-library/react-native-shake/releases),并下载适用版本的 tgz 包。 |
| 21 | + |
| 22 | + |
| 23 | +进入到工程目录并输入以下命令: |
| 24 | + |
| 25 | +> [!TIP] # 处替换为 tgz 包的路径 |
| 26 | +
|
| 27 | +<!-- tabs:start --> |
| 28 | + |
| 29 | +#### **npm** |
| 30 | + |
| 31 | +```bash |
| 32 | +npm install @react-native-oh-tpl/react-native-shake@file:# |
| 33 | +``` |
| 34 | + |
| 35 | +#### **yarn** |
| 36 | + |
| 37 | +```bash |
| 38 | +yarn add @react-native-oh-tpl/react-native-shake@file:# |
| 39 | +``` |
| 40 | + |
| 41 | +<!-- tabs:end --> |
| 42 | + |
| 43 | +下面的代码展示了这个库的基本使用场景: |
| 44 | + |
| 45 | +> [!WARNING] 使用时 import 的库名不变。 |
| 46 | +
|
| 47 | +```js |
| 48 | +import React, { useState } from 'react'; |
| 49 | +import { Text, View } from 'react-native' |
| 50 | +import RNShake from 'react-native-shake'; |
| 51 | + |
| 52 | +export function ShakeExample() { |
| 53 | + const [result, setResult] = useState<string>('') |
| 54 | + myComponent(setResult) |
| 55 | + return ( |
| 56 | + <View style={{backgroundColor:'white',width:'100%'}}> |
| 57 | + <Text>shake(摇晃手机)</Text> |
| 58 | + <Text>{result}</Text> |
| 59 | + </View> |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +export const myComponent = (setResult: React.Dispatch<React.SetStateAction<string>>) => { |
| 64 | + React.useEffect(() => { |
| 65 | + const subscription = RNShake.addListener(() => { |
| 66 | + setResult('shake listen success') |
| 67 | + }) |
| 68 | + return () => { |
| 69 | + subscription.remove() |
| 70 | + } |
| 71 | + }, []) |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +``` |
| 76 | +## 使用 Codegen |
| 77 | + |
| 78 | +本库已经适配了 `Codegen` ,在使用前需要主动执行生成三方库桥接代码,详细请参考[ Codegen 使用文档](/zh-cn/codegen.md)。 |
| 79 | +## Link |
| 80 | + |
| 81 | +目前鸿蒙暂不支持 AutoLink,所以 Link 步骤需要手动配置。 |
| 82 | + |
| 83 | +首先需要使用 DevEco Studio 打开项目里的鸿蒙工程 `harmony` |
| 84 | + |
| 85 | +### 在工程根目录的 `oh-package.json` 添加 overrides字段 |
| 86 | + |
| 87 | +```json |
| 88 | +{ |
| 89 | + ... |
| 90 | + "overrides": { |
| 91 | + "@rnoh/react-native-openharmony" : "./react_native_openharmony" |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### 引入原生端代码 |
| 97 | + |
| 98 | +目前有两种方法: |
| 99 | + |
| 100 | +1. 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法); |
| 101 | +2. 直接链接源码。 |
| 102 | + |
| 103 | +方法一:通过 har 包引入(推荐) |
| 104 | + |
| 105 | +> [!TIP] har 包位于三方库安装路径的 `harmony` 文件夹下。 |
| 106 | +
|
| 107 | +打开 `entry/oh-package.json5`,添加以下依赖 |
| 108 | + |
| 109 | +```json |
| 110 | +"dependencies": { |
| 111 | + "@rnoh/react-native-openharmony": "file:../react_native_openharmony", |
| 112 | + "@react-native-oh-tpl/react-native-shake": "file:../../node_modules/@react-native-oh-tpl/react-native-shake/harmony/shake_package.har" |
| 113 | + } |
| 114 | +``` |
| 115 | + |
| 116 | +点击右上角的 `sync` 按钮 |
| 117 | + |
| 118 | +或者在终端执行: |
| 119 | + |
| 120 | +```bash |
| 121 | +cd entry |
| 122 | +ohpm install |
| 123 | +``` |
| 124 | + |
| 125 | +方法二:直接链接源码 |
| 126 | + |
| 127 | +> [!TIP] 如需使用直接链接源码,请参考[直接链接源码说明](/zh-cn/link-source-code.md)。 |
| 128 | +
|
| 129 | + |
| 130 | +### 在 ArkTs 侧引入 ShakePackage |
| 131 | + |
| 132 | +打开 `entry/src/main/ets/RNPackagesFactory.ts`,添加: |
| 133 | + |
| 134 | +```diff |
| 135 | +... |
| 136 | ++ import { ShakePackage } from "@react-native-oh-tpl/rnoh-shake-package/ts"; |
| 137 | + |
| 138 | +export function createRNPackages(ctx: RNPackageContext): RNPackage[] { |
| 139 | + return [ |
| 140 | + new SamplePackage(ctx), |
| 141 | ++ new ShakePackage(ctx) |
| 142 | + ]; |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### 运行 |
| 147 | + |
| 148 | +点击右上角的 `sync` 按钮 |
| 149 | + |
| 150 | +或者在终端执行: |
| 151 | + |
| 152 | +```bash |
| 153 | +cd entry |
| 154 | +ohpm install |
| 155 | +``` |
| 156 | + |
| 157 | +然后编译、运行即可。 |
| 158 | + |
| 159 | +## 约束与限制 |
| 160 | + |
| 161 | +### 兼容性 |
| 162 | + |
| 163 | + |
| 164 | +要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。 |
| 165 | + |
| 166 | +请到三方库相应的 Releases 发布地址查看 Release 配套的版本信息:[@react-native-oh-tpl/react-native-shake Releases](https://github.com/react-native-oh-library/react-native-shake/releases) |
| 167 | + |
| 168 | + |
| 169 | +### 权限要求 |
| 170 | + |
| 171 | +Add the following permissions to their respective files: |
| 172 | + |
| 173 | +In your `module.json5` |
| 174 | + |
| 175 | +``` |
| 176 | + "requestPermissions": [ |
| 177 | + { |
| 178 | + "name": "ohos.permission.ACCELEROMETER" |
| 179 | + } |
| 180 | + ] |
| 181 | +``` |
| 182 | + |
| 183 | +## API |
| 184 | + |
| 185 | +> [!tip] "Platform"列表示该属性在原三方库上支持的平台。 |
| 186 | +
|
| 187 | +> [!tip] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。 |
| 188 | +
|
| 189 | +| Name | Description | Type | Required | Platform | HarmonyOS Support | |
| 190 | +| ---- | ----------- | ---- | -------- | -------- | ------------------ | |
| 191 | +| addListener | add shake event listening | function | yes | ios/andriod | yes | |
| 192 | +| removeAllListeners | remove event listening | function | yes | ios/andriod | yes | |
| 193 | + |
| 194 | +## 遗留问题 |
| 195 | + |
| 196 | +## 其他 |
| 197 | + |
| 198 | +## 开源协议 |
| 199 | + |
| 200 | +本项目基于 [The MIT License (MIT)](https://github.com/Doko-Demo-Doa/react-native-shake/blob/main/LICENSE) ,请自由地享受和参与开源。 |
| 201 | +<!-- {% endraw %} --> |
0 commit comments