曾 俊

曾 俊

1659713760

在 React Native 中构建动画手风琴列表

手风琴列表(或ExpandableListViews)现在是材料设计的主要内容,并出现在很多应用程序中。它们是向用户动态显示信息的好方法。

在您自己的应用程序中实现一个时,当然,您可以使用一些库。但是自己实现一个可以让您更好地控制样式,更小的应用程序包大小,而且它非常简单,甚至可能更快!这也是一个使用 React Native Hooks 和 Animation API 提高技能的好机会。

这就是我们将要创建的:

手风琴列表最终结果

只要给我看代码!

好吧好吧!这里有点心。不过考虑留下来——你可能会学到一些东西。

让我们开始吧

首先,我们需要一个功能组件。这将需要两个参数或道具:

  1. 一个string。列表项顶部栏中的标题。
  2. 一个ReactNode。可展开部分中的组件。

对于其中的第二个,React 允许我们通过this.props.children类组件访问子组件,或者我们可以将子组件指定为函数组件的参数。魔法!

所以我们的功能组件具有以下形式:

const AccordionListItem = ({ title, children }) => {
 ...
};
export default AccordionListItem;

我们可以像这样使用它:

<AccordionListItem title={"List Item"}>
  <Text>Some body text!</Text>
</AccordionListItem>

我们列表项的顶部栏需要用 a 包裹,TouchableWithoutFeedback因为我们希望用户能够通过按下它来打开和关闭列表项。这可以包含我们通过道具传入的标题。

<TouchableWithoutFeedback>
  <View style={styles.titleBackground}>
    <Text>{title}</Text>
  </View>
</TouchableWithoutFeedback>

现在让我们View为列表项正文添加一个。这是单击标题时滑入和滑出的部分,将包含我们的子组件。

<View style={styles.bodyBackground}>
  {children}
</View>

让我们看看到目前为止我们有什么:

手风琴列表没有动画

没什么革命性的吧?忍受我,这就是有趣的地方!

现在我上瘾了

接下来我们要添加一些 React 钩子来跟踪我们组件的状态。他们来了:

  1. Astate跟踪项目是打开还是关闭。
  2. Animated.Value动画身体部分的打开和关闭。
  3. 身体部分的高度——这是我们将要改变的,以便显示和隐藏它。

或以代码形式:

const [open, setOpen] = useState(false);
const animatedController = useRef(new Animated.Value(0)).current;
const [bodySectionHeight, setBodySectionHeight] = useState(0);

请注意,我们useRef用于创建动画对象。由于我们的打开状态将在组件刷新期间保持不变,我们希望我们的动画值也这样做,否则它们将不同步!useRef充当我们的一种状态容器Animated.Value,将其提升到组件生命周期之外。

手风琴时间!

现在我们需要将我们绑定Animated.Value到 body 视图的高度。我们的动画值可以在我们想要的任何范围内变化,但最容易选择一个简单的范围,如 [0, 1]。在这里,0 映射到正在关闭的列表项,而 1 映射到正在打开的列表项。随着动画值从 0 -> 1 变化,我们希望视图的高度从 0 -> 变化bodySectionHeight。为此,我们可以使用以下interpolate功能:

const bodyHeight = animatedController.interpolate({
  inputRange: [0, 1],
  outputRange: [0, size.height],
});

这只是将 映射inputRange到与outputRange我们描述的完全一致。当我们更改 的值时animatedController,该bodyHeight变量将相对于容器的高度缩放该值。完成所有这些设置后,我们现在可以编写toggleListItem()函数,当我们单击项目标题时将调用该函数。

我们可以这样思考这个函数要做的事情:

  1. 如果列表项打开
    :将动画值从 1 更改为 0
    。将状态设置为关闭。
  2. 如果列表项已关闭
    :将动画值从 0 更改为 1
    。将状态设置为打开。
const toggleListItem = () => {
  if (open) {
    Animated.timing(animatedController, {
      duration: 300,
      toValue: 0,
    }).start();
  } else {
    Animated.timing(animatedController, {
      duration: 300,
      toValue: 1,
    }).start();
  }
  setOpen(!open);
};

我已将持续时间设置为 300 毫秒,但您可以四处寻找您喜欢的速度。

我们已经很接近了。为了使用 React Native 的动画系统,我们需要创建我们View的 an Animated.View,然后将动画视图的高度设置为bodyHeight变量。

<Animated.View style={[styles.bodyBackground, { height: bodyHeight }]}>
  {children}
</Animated.View>

现在是偷偷摸摸的一步。我们需要为我们的高度设置一个初始值,bodyView这样我们就知道当我们打开列表项时要返回什么值。但是,我们不能使用动画视图的高度。这是因为我们要设置bodyHeightusing 的onLayoutprop View。对于动画视图,这在视图动画时会被多次调用!这对我们没有好处,我们只想设置一次。

为了解决这个问题,我们可以在动画视图中嵌套另一个常规视图,它可以具有恒定的高度。然后我们可以使用这个视图的高度作为 的值bodyHeight

onLayout接受一个带有事件参数的函数。我们通过 访问组件高度event.nativeEvent.layout.height。所以,把它们放在一起:

<Animated.View style={[styles.bodyBackground, { height: bodyHeight }]}>
  <View onLayout={setSize} style={styles.bodyContainer}>
    {children}
  </View>
</Animated.View>

最后我们需要设置样式的overflow: “hidden”属性。bodyBackground这告诉动画视图的子视图如果溢出视图框该怎么办,这正是列表项关闭时会发生的情况。将此设置为隐藏意味着它们将不可见,完美!

非常好!

逼真的幻灯片

我们所拥有的很好,但如果我们可以给人一种列表项的主体从标题栏展开的印象,那就太好了。幸运的是,有一种简单的方法可以实现这一点!我们可以在bodyContainer样式中添加以下属性:

position: "absolute";
bottom: 0;

这确保了内部视图的底部始终与外部视图的底部对齐,这意味着内部视图应该随着列表项的打开和关闭而很好地上下滑动。

旋转箭头

另一个不错的功能是有一个箭头指示器,它随着列表项的打开和关闭而旋转。有了我们对 React Native 动画的新知识,这应该很容易!由于我们的动画控制器从 0 -> 1 变化,我们希望箭头的角度在 0 -> π 弧度或 0 -> 180° 之间变化(对于普通人)。使用相同的动画控制器意味着它将始终与打开和关闭同步。所以我们有:

const arrowAngle = animatedController.interpolate({
  inputRange: [0, 1],
  outputRange: ["0rad", `${Math.PI}rad`],
});

然后我们可以将我们选择的箭头图标添加Animated.View到我们的标题容器视图中:

<View style={styles.titleBackground}>
  <View style={styles.titleContainer}>
    <Text style={styles.titleText}>{title}</Text>
    <Animated.View style={{ transform: [{ rotateZ: arrowScale }] }}>
      <DownArrowSVG width={20} height={28} />
    </Animated.View>
</View>

在这里,我们使用transformCSS 属性来传递视图的 z 旋转角度(换句话说,垂直于 z 轴的角度 - 我必须先尝试解决这个问题rotateYrotateX)。

放轻松

一个为真正的完美主义者准备的。easingReact Native 允许我们通过参数来改变动画的速度。在我们的例子中,这是一个微妙的变化,但这是一个很好的习惯,因为它可以对某些动画的感觉产生很大的影响。

大多数时候我使用标准的 Material Design 缓动:

easing: Easing.bezier(0.4, 0.0, 0.2, 1);

把它放在我们的两个Animated.timing配置中,我们得到了我们的成品!

全部完成!

即使完成了所有这些工作,我们的列表项在 gzip 压缩后的大小也小于 1kb!我挑战你找到一个更小的图书馆。我们还可以完全控制样式的各个方面。将其粘贴在一个FlatList组件中以创建一个漂亮的可滚动列表,或者如果您只需要几个,您可以像常规组件一样堆叠它们。玩得开心!

来源:https ://blog.theodo.com/2020/06/build-accordion-list-react-native/

#react-native 

What is GEEK

Buddha Community

在 React Native 中构建动画手风琴列表
Autumn  Blick

Autumn Blick

1598839687

How native is React Native? | React Native vs Native App Development

If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?

In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.

A brief introduction to React Native

Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.

React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.

Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.

Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.

The popularity of React Native comes from its advantages. Some of its advantages are as follows:

  • Performance: It delivers optimal performance.
  • Cross-platform development: You can develop both Android and iOS apps with it. The reuse of code expedites development and reduces costs.
  • UI design: React Native enables you to design simple and responsive UI for your mobile app.
  • 3rd party plugins: This framework supports 3rd party plugins.
  • Developer community: A vibrant community of developers support React Native.

Why React Native is fundamentally different from earlier hybrid frameworks

Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.

React Native is very close to native. Consider the following aspects as described on the React Native website:

  • Access to many native platforms features: The primitives of React Native render to native platform UI. This means that your React Native app will use many native platform APIs as native apps would do.
  • Near-native user experience: React Native provides several native components, and these are platform agnostic.
  • The ease of accessing native APIs: React Native uses a declarative UI paradigm. This enables React Native to interact easily with native platform APIs since React Native wraps existing native code.

Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.

#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms?

React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. Hire Dedicated React Native Developer from top React Native development company, HourlyDeveloper.io to design a spectacular React Native application for your business.

Consult with experts:- https://bit.ly/2A8L4vz

#hire dedicated react native developer #react native development company #react native development services #react native development #react native developer #react native

Hire Dedicated React Native Developers - WebClues Infotech

Being one of the emerging frameworks for app development the need to develop react native apps has increased over the years.

Looking for a react native developer?

Worry not! WebClues infotech offers services to Hire React Native Developers for your app development needs. We at WebClues Infotech offer a wide range of Web & Mobile App Development services based o your business or Startup requirement for Android and iOS apps.

WebClues Infotech also has a flexible method of cost calculation for hiring react native developers such as Hourly, Weekly, or Project Basis.

Want to get your app idea into reality with a react native framework?

Get in touch with us.

Hire React Native Developer Now: https://www.webcluesinfotech.com/hire-react-native-app-developer/

For inquiry: https://www.webcluesinfotech.com/contact-us/

Email: sales@webcluesinfotech.com

#hire react native developers #hire dedicated react native developers #hire react native developer #hiring a react native developer #hire freelance react native developers #hire react native developers in 1 hour

Factors affecting the cost of hiring a React Native developer in USA - TopDevelopers.co

Want to develop app using React Native? Here are the tips that will help to reduce the cost of react native app development for you.
Cost is a major factor in helping entrepreneurs take decisions about investing in developing an app and the decision to hire react native app developers in USA can prove to be fruitful in the long run. Using react native for app development ensures a wide range of benefits to your business. Understanding your business and working on the aspects to strengthen business processes through a cost-efficient mobile app will be the key to success.

#best react native development companies from the us #top react native app development companies in usa #cost of hiring a react native developer in usa #top-notch react native developer in usa #best react native developers usa #react native

How much does it cost to develop a React Native mobile app?

React Native allows developers to develop mobile apps that have compatibility with Android, iOS & other operating systems. Due to the features like Native-like functionality and single code reusability and the access of various frameworks in the market, React Native has excelled as the most suitable framework for cross-platform mobile app development.

Why Do Businesses Prefer React Native App Development?

React Native is integrated with JS library that works as the fundamental for developing the app UI. Most businesses choose for developing React Native apps just due to their cross-platform & open-source features. A few further reasons why entrepreneurs & developers choose React Native app development include:

• Lowered Expedition Time

• Simple UI

• Cross-Platform and Code Sharing

• Lesser Workforce and Resources

• Community Assistance

• In-Built Elements and Reusable Codes

• Hot Reload

• JavaScript as Programming Language

• Easy to Execute Updates

Factors That Decide Cost of React Native App Development

If you are an entrepreneur or start-up and looking for cost-effective app development, React Native is one of the ideal options available out there.

• App’s UI/UX Design

• User Authorization

• App Complexity and Functionality

• App Development Team

• App Maintenance

• App Add-ons

• App Distribution

• Location of Development Company

• App Category

React Native cost depends widely on the complexity of a project or the app requirements. The price may also vary based on business requirements. React Native app development per hour can cost from $20 and $30 per hour in India. It can vary as per different locations.

Is React Native a good choice for mobile apps development?

Yes, React Native is the best choice for mobile app development as React apps are faster to develop and it offers better quality than hybrid apps. Additionally, React Native is a mature cross-platform framework.

Best React Native App Development Agency

AppClues Infotech is a leading React Native App Development Company in USA that build robust & innovative mobile app as per your specific business needs. They have a dedicated team of designers and programmers help to make a perfect mobile app.

If you have any mobile app development project in mind get in touch with AppClues Infotech and get the best solution for your business.

#react native app development cost #react native development company #best react native development company in usa #hire react native developers #hire dedicated react native developers & programmers #hire a react native development company