基于 react-native-hole-view 的 OpenHarmony 适配版,挖孔视图组件(在遮罩层挖洞,用于新手引导)
Access rate limit exceeded. Please log in to continue.
Document Template: v0.4.2
react-native-hole-view
English | 简体中文
This project is based on react-native-hole-view.
This third-party library has been migrated to AtomGit and is now available for direct download from npm, the new package name is: @react-native-ohos/react-native-hole-view, the version correspondence details are as follows:
| Name | Version(Npm Address) | Release Information | Supported RN Version | Supported Autolink | Compile API Version | Community Baseline Version | Source code address |
|---|---|---|---|---|---|---|---|
| @react-native-ohos/react-native-hole-view | ~3.2.0 (In development) | AtomGit Releases | 0.82.* | Yes | API12+ | 3.0.1 | br_rnoh0.82 |
| @react-native-ohos/react-native-hole-view | ~3.1.0 | AtomGit Releases | 0.77.* | No | API12+ | 3.0.1 | br_rnoh0.77 |
| @react-native-ohos/react-native-hole-view | ~3.0.1 | AtomGit Releases | 0.72.* | Yes | API12+ | 3.0.0-alpha4 | br_rnoh0.72 |
| @react-native-oh-tpl/react-native-hole-view | <= 3.0.0-alpha4-0.0.5@deprecated | Github Releases(deprecated) | 0.72.* | No | API12+ | 3.0.0-alpha4 | sig |
Introduction
react-native-hole-view is a view component for React Native. It cuts holes with specified position, size and corner radius in an overlay view. The area inside a hole reveals the content underneath and supports touch pass-through, which is commonly used in scenarios such as guide masks and highlight tips.
Installation
Go to the project directory and execute the following instruction:
npm
npm install @react-native-ohos/react-native-hole-view
yarn
yarn add @react-native-ohos/react-native-hole-view
Link
| Version | Supported Autolink | Supported RN Version |
|---|---|---|
| ~ 3.2.0 | Yes | 0.82 |
| ~ 3.1.0 | No | 0.77 |
| ~ 3.0.1 | Yes | 0.72 |
| <= 3.0.0-alpha4-0.0.5@deprecated | No | 0.72 |
Projects using Autolink need to be configured according to this document, Autolink framework guide: https://atomgit.com/CPF-RN/ohos_react_native/blob/main/docs/en/02-development/02-development-guide/autolinking.md .
If the version you are using supports Autolink and the project has integrated Autolink, you can skip the ManualLink configuration.
ManualLink: This step provides guidance for manually configuring native dependencies.
Open the harmony directory of the OpenHarmony project in DevEco Studio.
1. Overrides RN SDK
To ensure the project relies on the same version of the RN SDK, you need to add an overrides field in the project's root oh-package.json5 file, specifying the RN SDK version to be used. The replacement version can be a specific version number, a semver range, or a locally available HAR package or source directory.
For more information about the purpose of this field, please refer to the official documentation.
{
"overrides": {
"@rnoh/react-native-openharmony": "file:../node_modules/@react-native-oh/react-native-harmony/react_native_openharmony.har" // a locally available HAR package
// "@rnoh/react-native-openharmony": "~0.82.18" // ohpm version
// "@rnoh/react-native-openharmony": "./react_native_openharmony" // source code directory
}
}
2. Introducing Native Code
There are two methods:
- Introduce through the har package;
- Directly link to the source code.
Method 1: Introduce through the har package (recommended)
The har package is located in the `harmony` folder of the third-party library installation path.
Open entry/oh-package.json5 and add the following dependency:
"dependencies": {
"@react-native-ohos/react-native-hole-view": "file:../../node_modules/@react-native-ohos/react-native-hole-view/harmony/rnoh_holeview.har"
}
Click the sync button in the upper right corner.
Alternatively, run the following instruction on the terminal:
cd entry
ohpm install
Method 2: Directly link to the source code.
For details, see [Directly Linking Source Code](https://atomgit.com/CPF-RN/usage-docs/blob/master/en/link-source-code.md).
3. Configuring CMakeLists and Introducing HoleViewPackage
Open entry/src/main/cpp/CMakeLists.txt and add the following code:
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
set(LOG_VERBOSITY_LEVEL 1)
set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
add_compile_definitions(WITH_HITRACE_SYSTRACE)
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
# RNOH_BEGIN: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-hole-view/src/main/cpp" ./hole_view)
# RNOH_END: manual_package_linking_1
file(GLOB GENERATED_CPP_FILES "./generated/*.cpp")
add_library(rnoh_app SHARED
${GENERATED_CPP_FILES}
"./PackageProvider.cpp"
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)
# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_holeview)
# RNOH_END: manual_package_linking_2
Open entry/src/main/cpp/PackageProvider.cpp and add the following code:
#include "RNOH/PackageProvider.h"
#include "SamplePackage.h"
+ #include "HoleViewPackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<SamplePackage>(ctx),
+ std::make_shared<RNOHHoleViewPackage>(ctx)
};
}
Running
Click the sync button in the upper right corner.
Alternatively, run the following instruction on the terminal:
cd entry
ohpm install
Then build and run the code.
Constraints
Compatibility
This document is verified based on the following versions:
- RNOH: 0.82.7; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1.260; ROM: 6.0.0.130 SP15;
Permission Requirements
No special permission is required.
Example
The following code shows the basic use scenario of the repository:
The name of the imported repository remains unchanged.
import React, {useCallback, useEffect, useState} from 'react';
import {
ScrollView,
View,
Text,
TouchableOpacity,
} from 'react-native';
import {RNHole, RNHoleView, ERNHoleViewTimingFunction, IRNHoleViewAnimation} from "react-native-hole-view";
const firstHole: RNHole = {x: 150, y: 390, width: 120, height: 120, borderRadius: 60};
const secondHole: RNHole = {x: 150, y: 40, width: 120, height: 120, borderRadius: 60};
const animationSettings: IRNHoleViewAnimation = {timingFunction: ERNHoleViewTimingFunction.EASE_IN_OUT, duration: 200};
const App = () => {
const [holes, setHoles] = useState<RNHole[]>([]);
const [animated, setAnimated] = useState<boolean>(false);
const [animation, setAnimation] = useState<IRNHoleViewAnimation | undefined>(undefined);
const onPress = useCallback(() => {
if (animated) {
setHoles([firstHole]);
} else {
setHoles([secondHole])
}
setAnimation({...animationSettings});
setAnimated(!animated);
}, [animated, animation])
useEffect(() => {
onPress();
}, []);
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<Text style={{flexGrow: 0, flex: 0, padding: 10}}>{"Wow! I'm a text inside a hole!"}</Text>
<TouchableOpacity onPress={() => {
}} style={{backgroundColor: 'pink', padding: 10, borderRadius: 5}}>
<Text>{"Wow! I'm a button inside a hole!"}</Text>
</TouchableOpacity>
<ScrollView style={{flexGrow: 0, flex: 0, padding: 10}} horizontal={true}>
<Text numberOfLines={1}>
{
"Wow! I'm a ScrollView inside a hole! Wow! I'm a ScrollView inside a hole! Wow! I'm a ScrollView inside a hole!"
}
</Text>
</ScrollView>
<RNHoleView
style={{
position: 'absolute',
width: '100%',
height: '100%',
backgroundColor: 'rgba(34,146,231,0.4)'
}}
animation={animation}
holes={holes}
onAnimationFinished={() => {
setAnimation(undefined);
}}
>
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<Text>{"Content inside the overlay view"}</Text>
</View>
</RNHoleView>
<View
pointerEvents={'box-none'}
style={{
position: 'absolute',
flex: 1,
width: '100%',
height: '100%',
alignItems: 'flex-end',
flexDirection: 'row',
justifyContent: 'center'
}}>
<TouchableOpacity onPress={onPress}
style={{backgroundColor: 'pink', padding: 10, borderRadius: 5, bottom: 50}}>
<Text>{"Animate!"}</Text>
</TouchableOpacity>
</View>
</View>
);
};
export default App;
How to Use
Basic usage
import { RNHoleView } from 'react-native-hole-view';
<RNHoleView
style={{ position: 'absolute', width: '100%', height: '100%', backgroundColor: 'rgba(0,0,0,0.5)' }}
holes={[{x: 50, y: 50, width: 100, height: 100, borderRadius: 50}]}
/>
Hole animation
import { ERNHoleViewTimingFunction } from 'react-native-hole-view';
<RNHoleView
holes={[{x: 50, y: 50, width: 100, height: 100}]}
animation={{ duration: 200, timingFunction: ERNHoleViewTimingFunction.EASE_IN_OUT }}
onAnimationFinished={() => {
// Triggered when the hole animation finishes
}}
/>
Available APIs
The "Platform" column indicates the platform where the properties are supported in the original third-party library.
If the value of "OpenHarmony Platform Support" is "Yes", it means that the OpenHarmony platform supports this property; "No" means the opposite; "Partially" means some capabilities of this property are supported. The usage method is the same on different platforms and the effect is the same as that of iOS or Android.
Components
| Name | Parameter Type | Required | Platform | OpenHarmony Platform Support | Description |
|---|---|---|---|---|---|
| RNHoleView | IRNHoleView | No | iOS, Android | Yes | A view component that cuts holes in an overlay view |
Properties
IRNHoleView
| Name | Parameter Type | Default Value | Required | Platform | OpenHarmony Platform Support | Description |
|---|---|---|---|---|---|---|
| holes | RNHole[] | None | No | iOS, Android | Yes | Hole array. Each hole is described by fields such as x, y, width, height and borderRadius |
| animation | IRNHoleViewAnimation | None | No | iOS, Android | Yes | Hole update animation effect, described by duration and timingFunction (ERNHoleViewTimingFunction enum) |
| onAnimationFinished | function | None | No | iOS, Android | Yes | Callback triggered when the hole animation finishes |
Known Issues
- The C API OH_ArkUI_PointerEvent_SetStopPropagation prevents event bubbling and cannot control the scrollView. As a result, the scrollView can also scroll outside the hole: issue#2
Other
None
Directory Structure
/rntpc_react-native-hole-view # Project root directory
├── harmony # HarmonyOS adaptation code
│ ├── rnoh_holeview.har # har package
│ └── rnoh_holeview # Core HarmonyOS adaptation code
│ ├── Index.ets # HarmonyOS adaptation code entry
│ └── src/main
│ ├── cpp # C++ adaptation code
│ │ ├── HoleViewPackage.h # C++ Package header file
│ │ └── RNHoleViewComponentInstance.h # RNHoleView component implementation
│ ├── resources # Resource files
│ └── module.json5 # Module configuration file
├── src # RN code
│ ├── index.ts # Library entry file
│ └── index.harmony.tsx # HarmonyOS-side component wrapper
├── package.json # Package management configuration file
├── README.OpenSource # Open-source information statement
├── README.md # Chinese document
├── README_en.md # English document
└── tsconfig.json # TypeScript configuration file
How to Contribute
If you find any problem during the use, you can submit an Issue. Of course, we also welcome you to submit a PR.
License
This project is based on The ISC License (ISC). Feel free to enjoy and participate in open source.