使用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.pswp.cn/news/395257.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395257.shtml
英文地址,請注明出處:http://en.pswp.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;每種日期和時…