使用React Native进行气泡动画

by Narendra N Shetty

由纳伦德拉·N·谢蒂(Narendra N Shetty)

使用React Native进行气泡动画 (Bubble animation with React Native)

使用AnimatedPanResponder构建React Native应用程序时获得的经验教训 (Lessons learned while building a React Native App using Animated and PanResponder)

In this article, I’ll talk about how I implemented an app transition which I found on Dribbble by Ramotion.

在本文中,我将讨论如何实现由Ramotion在Dribbble上发现的应用程序过渡。

This pagination controller can be used for onboarding flow or for a tutorial.

该分页控制器可用于入门流程或教程。

The complete version is published in Expo, and you can get it by opening the Expo app and scanning this QR code:

完整版本发布在Expo中,您可以通过打开Expo应用并扫描以下QR码来获取它:

让我们开始吧? (Let’s begin, shall we?)

Here’s how I built the background:

这是我建立背景的方式:

I had View holding the background color. This includes Animated.View for the bubble animation. Its position was absolute so that it stayed on top of the screen. I also added some basic styles.

我让View保持了背景色。 这包括气泡动画的Animated.View 。 它的位置是绝对的,因此它保持在屏幕顶部。 我还添加了一些基本样式。

Then, I animated the bubble by expanding from 0 to max using the CSS transform scale with Animated.timing.

然后,我通过使用Animated.timingCSS变换比例将气泡从0扩展到最大值,从而对气泡进行了Animated.timing

The above animation needed to trigger based on user interaction. First I used TouchableWithoutFeedback. Then I changed it with gestures.

上面的动画需要基于用户交互来触发。 首先,我使用了TouchableWithoutFeedback 。 然后我用手势更改了它。

I positioned the bubble according to the gif, which animated from the bottom. I did this using top and left property.

我根据从底部开始动画的gif定位气泡。 我使用topleft属性来完成此操作。

Neat! Results are as expected except the color. We’ll get back to that later.

整齐! 结果与预期的一样,除了颜色。 我们稍后再讲。

Now I restructured the code by moving the bubble logic into a separate component called CircleTransition. Then I triggered the animation from the parent component.

现在,我通过将气泡逻辑移到称为CircleTransition.的单独组件中来重组代码CircleTransition. 然后,我从父组件触发了动画。

Then it was time when to tackle the color. To make the bubble animate with the new color, I passed the new color into the component. And after the transition, I hid the bubble.

然后是时候解决颜色了。 为了使气泡以新颜色动画,我将新颜色传递到组件中。 过渡之后,我躲了起来。

Can you see something weird in the above transition?

在上述转换中,您能看到一些奇怪的东西吗?

During the second bubble transition, the background color is fell back to the first color. I needed to update the background color to the new color with the bubble transitioned.

在第二个气泡过渡期间,背景颜色会变回第一种颜色。 我需要在过渡气泡后将背景色更新为新颜色。

I passed a callback to the start method which executed when the transition completed.

我将回调传递给start方法,该方法在过渡完成时执行。

Voila! It’s all set. Now I had the basic animation working.

瞧! 都准备好了 现在,我可以使用基本的动画了。

Next I got into the gesture. The bubble transitions when the user swipes left or right according to the gif.

接下来,我进入了手势。 当用户根据gif左右滑动时,气泡会转变。

I created a new component called Swipe. It contains all the logic for the gesture and replacesTouchableWithoutFeedback.

我创建了一个名为Swipe的新组件。 它包含手势的所有逻辑,并替换了TouchableWithoutFeedback

I used PanResponderwhich reconciles several touches into a single gesture. It makes single-touch gestures resilient to extra touches. It can also recognize simple multi-touch gestures. For more on this you can go here and here.

我使用了PanResponder ,它将多个触摸调和为一个手势。 它使单点触摸手势具有额外的触摸弹性。 它还可以识别简单的多点触摸手势。 有关更多信息,您可以在这里和这里 。

Now for the logic of the gestures.

现在了解手势的逻辑。

First I needed to figure out which direction the user is swiping. I only need to animate when the user swipes left or right. I also needed to setup some threshold to determine if it’s actually a swipe or not.

首先,我需要弄清楚用户向哪个方向滑动。 当用户向左或向右滑动时,只需要设置动画即可。 我还需要设置一些阈值,以确定是否实际上是刷卡。

If it was a valid swipe, I triggered the appropriate action.

如果刷卡有效,则触发了相应的操作。

Yes! You guessed it right. I have gotten what I wanted to achieve with the gesture. Then I added an action for swipeRight. The gesture was completed with a bit of refactoring.

是! 你猜对了。 我已经通过手势获得了想要实现的目标。 然后我为swipeRight添加了一个动作。 手势完成了一点重构。

That’s it! This was the most complex part in the app.

而已! 这是应用程序中最复杂的部分。

I won’t show my complete work on this app. The information in this post should be enough help you build your own. Fork this and try to complete your app to match the above gif.

我不会在此应用程序上显示我的完整作品。 这篇文章中的信息应该足以帮助您构建自己的信息。 对此进行分叉,并尝试完成您的应用以匹配上述gif。

If you are stuck and need any help, final version is in result branch, have a peek. Also you can ping me on Twitter @narendra_shetty or comment below.

如果您遇到困难并需要任何帮助,则最终版本在result分支中,请查看。 您也可以在Twitter @narendra_shetty上对我ping或在下面发表评论。

And when you complete, please share your Expo/GitHub link.

完成后,请分享您的Expo / GitHub链接。

If this article was of any help to you, please clap for me. It will motivate me to write more :)

如果这篇文章对您有帮助,请为我鼓掌。 这会激励我写更多的东西:)

翻译自: https://www.freecodecamp.org/news/bubble-animation-with-react-native-72674eab073a/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/395257.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Machine Learning from Start to Finish with Scikit-Learn

2019独角兽企业重金招聘Python工程师标准>>> Machine Learning from Start to Finish with Scikit-Learn This notebook covers the basic Machine Learning process in Python step-by-step. Go from raw data to at least 78% accuracy on the Titanic Survivors …

Excel 宏编码实现,指定列的字符串截取

1、打开Excel凭证,启用宏,ALTF11 或 菜单“视图”-"宏-查看宏" Sub 分割字符串1() Dim i As Integer Dim b() As String Dim length 用length表示数组的长度 Dim sublength Dim bb() As String 筛选日期 2 点 For i 2 To 20000 b() Split(Ce…

mysql for update 锁_MySql FOR UPDATE 锁的一点问题……

问题描述假设一个情况,这里只是假设,真实的情况可能不会这样设计,但是假如真的发生了....铁老大有一张这样的ticket表,用来存放北京到上海的票。iduidstart_addrend_addrbook_time11300009860上海北京1386666032120上海北京30上海…

服务器机房新风系统,某机房新风系统设计方案参考

《某机房新风系统设计方案参考》由会员分享,可在线阅读,更多相关《某机房新风系统设计方案参考(3页珍藏版)》请在人人文库网上搜索。1、某机房新风系统设计方案参考根据以上要求并结合中华人民共和国电子计算机机房的设计规范,为保证机房正压…

css 画三角形

CSS三角形绘制方法#triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;}#triangle-down {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid trans…

面试官面试前端_如何面试面试官

面试官面试前端by Aline Lerner通过艾琳勒纳(Aline Lerner) 如何面试面试官 (How to interview your interviewers) For the last few semesters, I’ve had the distinct pleasure of guest-lecturing MIT’s required technical communication class for computer science m…

shell 字符串分割

语法1: substring${string:start:len} string的下标从0开始,以start可是,截取len个字符,并赋值于substring 1 #!/bin/bash 2 #substr${string:start:len} 3 str"123456789" 4 substr${str:3:3} 5 echo $substr 6 7 输出&#xff1…

方格取数(网络流)

题目链接:ヾ(≧∇≦*)ゝ 大致题意:给你一个\(n*m\)的矩阵,可以取任意多个数,但若你取了一个数,那么这个数上下左右的数你就都不能取,问能取到的最大值是多少。 Solution: 首先,我们可以把矩阵上…

mysql创建的数据库都在哪里看_mysql 怎么查看创建的数据库和表

1、 //看当前使用的是哪个数据库 ,如果你还没选择任何数据库,结果是NULL。mysql>select database(); ------------ | DATABASE() | ------------ | menagerie | ------------2、//查看有哪些数据库 mysql> show databases;--------------------| Database …

wordpress 基础文件

需要用到的PHP基础文件有&#xff1a; 404.php404模板 rtl.css 如果网站的阅读方向是自右向左的&#xff0c;会被自动包含进来comments.php 评论模板single.php文章模板。显示单独的一篇文章时被调用&#xff0c;如果模板不存在会使用 index.phpsingle-<post-type>.php自…

ajax请求 apend,jsp如何获取ajax append的数据?

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼我在网上下了个上传图片的js&#xff0c;我想上传图片的时候还提交一些参数&#xff0c;但是后台用request.getParameter("th");获取出来是nullfunction uploadSubmitHandler () {if (state.fileBatch.length ! 0) {var …

linux 机器格式化_为什么机器人应该为我们格式化代码

linux 机器格式化by Artem Sapegin通过Artem Sapegin 为什么机器人应该为我们格式化代码 (Why robots should format our code for us) I used to think that a personal code style is a good thing for a programmer. It shows you are a mature developer who knows what g…

Pytest高级进阶之Fixture

From: https://www.jianshu.com/p/54b0f4016300 一. fixture介绍 fixture是pytest的一个闪光点&#xff0c;pytest要精通怎么能不学习fixture呢&#xff1f;跟着我一起深入学习fixture吧。其实unittest和nose都支持fixture&#xff0c;但是pytest做得更炫。 fixture是pytest特有…

mysql 慢日志报警_一则MySQL慢日志监控误报的问题分析

之前因为各种原因&#xff0c;有些报警没有引起重视&#xff0c;最近放假马上排除了一些潜在的人为原因&#xff0c;发现数据库的慢日志报警有些奇怪&#xff0c;主要表现是慢日志报警不属实&#xff0c;收到报警的即时通信提醒后&#xff0c;隔一会去数据库里面去排查&#xf…

用css实现自定义虚线边框

开发产品功能的时候ui往往会给出虚线边框的效果图&#xff0c;于是乎&#xff0c;我们往往第一时间想到的是用css里的border&#xff0c;可是border里一般就提供两种效果&#xff0c;dashed或者dotted&#xff0c;ui这时就不满意了&#xff0c;说虚线太密了。废话不多说&#x…

无限复活服务器,绝地求生无限复活模式怎么玩 无限复活新手教程

相信不少的绝地求生玩家们最近都听说了其无限复活模式吧?因此肯定想要知道这种模式究竟该怎么玩&#xff0c;所以下面就来为各位带来此玩法的攻略相关&#xff0c;希望各位在看了如下的内容之后恩呢狗狗了解到新手教程攻略一览。“War”模式的设定以及玩法规则如下&#xff1a…

lua math.random()

math.random([n [,m]]) 用法&#xff1a;1.无参调用&#xff0c;产生[0, 1)之间的浮点随机数。 2.一个参数n&#xff0c;产生[1, n]之间的整数。 3.两个参数&#xff0c;产生[n, m]之间的整数。 math.randomseed(n) 用法&#xff1a;接收一个整数n作为随即序列的种子。 例&…

零基础学习ruby_学习Ruby:从零到英雄

零基础学习ruby“Ruby is simple in appearance, but is very complex inside, just like our human body.” — Matz, creator of the Ruby programming language“ Ruby的外观很简单&#xff0c;但是内部却非常复杂&#xff0c;就像我们的人体一样。” — Matz &#xff0c;R…

windows同时启动多个微信

1、创建mychat.bat文件(文件名任意)&#xff0c;输入以下代码&#xff0c;其中"C:\Program Files (x86)\Tencent\WeChat\"为微信的安装路径。以下示例为同时启动两个微信 start/d "C:\Program Files (x86)\Tencent\WeChat\" Wechat.exe start/d "C:\P…

mysql date time year_YEAR、DATE、TIME、DATETIME和TIMESTAMP详细介绍[MySQL数据类型]

为了方便在数据库中存储日期和时间&#xff0c;MySQL提供了表示日期和时间的数据类型&#xff0c;分别是YEAR、DATE、TIME、DATETIME和TIMESTAMP。下面列举了这些MSL中日期和时间数据类型所对应的字节数、取值范围、日期格式以及零值。从上图中可以看出&#xff0c;每种日期和时…