by Pritish Vaidya

How to build a flip timer in React Native

1VV6FGL7ptEwZZ4l-F1GmXVQfUcBzapWp8D2
Photo by Vico Luna on Unsplash

Introduction

A Flip Timer is a digital time keeping device with the time indicated by numbers that are sequentially revealed by a split-flap display.

This article will demonstrate the building of a Flip Timer in React Native using its exposed APIs and no additional dependencies.

Challenges to overcome

  • Implement transform-origin property using your College Math Course matrices techniques since it is not supported in React Native. Rotation around the centered origin (by default) is easy, but we need to translate origin and rotate around the edges.
  • Implementation of Flip Number component.
  • Overcome overflow: hidden issue in android since it doesn’t work with absolute positioned elements.

Contents

  1. Implement Flip Number Component
  2. Implement FoldView
  • Basic Layout
  • Overcoming the Challenge
  • Adding the Transformations
  • Adding the Animations

3. Update Timer Component

4. Final Result

5. Links

Implement Flip Number Component

Basic Layout

The Basic Layout consists of two cards — upper and lower joined together so that the view looks like a Flip Timer.

Number Card

It is a basic layout consisting of a wrapper and two cards — lower, upper.

Note: Lower Card has the previous number added to it. Its use will be revealed once we reach the FoldView implementation.

uzy-CsNuZji7eCwWTu6LYYOhygfzB4uP7v68
Image Credit: Carbon. | Code: GitHub

Card

The wrapper of the card has overflow: hidden, and we’re translating its items (number) based on the type of the card (upper or lower).

-1JOq61At7-JFxeyTRsn5vgtzCZYTL9UkZ-M
Image Credit: Carbon. | Code: GitHub

Implement FoldView

Basic Layout

To build FoldView we need two FlipCards similar to NumberCards but with absolute positioning so that they are above the NumberCards when flip animations are applied.

Number Card

Adding FlipCard component to the NumberCard component.

YrmnGEZBPPoxwsvskGYqHaYzPJ4VkB8viPO1
Image Credit: Carbon. | Code: GitHub

Flip Card

The FlipCard component is an animated wrapper with absolute positioning used while applying flip animation.

Challenge (Part 2 and Part 3): overflow: hidden with absolute positioning has major issues in android. Using this StackOverflow post, it can be solved by using an overflow container inside the absolute positioned element.

KG-NyI8-vafcBiZGWoZaD1hpCpwW0V-3ff3a
Image Credit: Carbon. | Code: GitHub

Final Result

qCA0sQ1vuVxxPDWMqOirMoOXtPhRh2-Rwyrl

Overcoming the Challenge

Now comes the hard part. We need to add fold the FlipCard component along the edges.

Since React Native doesn’t support transform-origin property, we need to find some other way to shift the rotation origin on the bottom edge.

Fortunately, there is one way to overcome this issue. According to this awesome article and reading the MDN docs for the transform-origin property, it can be implemented using matrices.

Utils

React Native exposes several matrix operations in the MatrixMath.js. The important ones that we require are

  • Identity Matrix: It returns a 4 * 4 identity matrix [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
J7CO8Ge-QqrmmFwuphxEwbypurpEPgQFUqeQ
Image Credit: Carbon. | Code: GitHub
  • Multiply Matrix: This utility method generates output based on the multiplication of 4*4 matrices a and b supplied as input.
1xbhTbClhu47mEsGPa7p4T7wHsajXl3PAy0J
Image Credit: Carbon. | Code: GitHub
  • Rotate Matrix: It is a custom utility method that will take a 4*4 matrix and degree to which it will be rotated to then multiply it to the original matrix to return the generated result.
qa2oiTgiOehlLCu4Qqfcj6zhNofoKpfA-1XL
q2MBhiltQRvtPmHC8r28UGvBDGPYOSTyEriN
Image Credit: Carbon. | Code: GitHub
  • Perspective Matrix: This utility method will allow us to use the perspective style to React Native and then multiply to the original 4*4 matrix.
0HmvXN1gu3-hGoK1ydxmf-8rBmxtIJEe3PNi
RjIzOwoDY1-1vvH1Y9lu7RC-opK8hpS1vSoL
Image Credit: Carbon. | Code: GitHub
  • Translate Matrix: This utility method will translate the origin and modify the original 4*4 matrix
OwRIvTNQ6f4YHisOmGSU362njvxovBFJ8JVy
ThtIdQR5osqzP5GMmxFGvQB5oUbuUUHMMwRv
Image Credit: Carbon. | Code: GitHub
  • Un-Translate Matrix: This utility method will un-translate the origin and modify the original 4*4 matrix
P0Ct10em0E2zebJeY2UTdZKMW2JmWyM62xMJ
Image Credit: Carbon. | Code: GitHub

Adding the Transformations

deg is the degree to be rotated and y is the height of the component to which it will be translated.

Challenge (Part 1): Combining the utils from the above, transform-origin is implemented successfully.

e9M2yLlahuXIQJ03D3UdvbQSNxSLlY-Lppdk
2qvRlyie7oBWQUwfyOn1t7RAvnZQ-rALAyj9
Image Credit: Carbon. | Code: GitHub

Adding the Animations

GBeUP2fLLmHCcbS6gpVdXBR1DwRMuULV2t2t
Image Credit: Carbon. | Code: GitHub

Update Timer Component

Add Time Util

This util will increment the timer by one sec and adjust hours, minutes, seconds.

3r0RZrCsJG4d0J3VNbjKaO4lPlySXO1Cxl0S
Image Credit: Carbon. | Code: GitHub

Timer Component

The timer component will call Time Util and update the component based on hours, minutes, seconds

ZcNz-7U5YSbU8UYyQwKijO8cfpEE2-CL1pN4
Image Credit: Carbon. | Code: GitHub

Flip Number Component

This component just splits the number into two parts based on their digit placement and calls NumberCard component .

MPgjFf9b9pYxOTVO461fBB-Dqh-8O25D3z5b
Image Credit: Carbon. | Code: GitHub

Final Result

c93caflCfnyqBNcQed-BOGQITYP-QFngekZ8

I’ve published a package for it that contains more customizable properties.

More of the cool stuff can be found on my StackOverflow and GitHub profiles.

Follow me on LinkedIn, Medium, Twitter for further update new articles.

One clap, two claps, three claps, forty?

6nEX6G3ucbC8JOm8KkdqSWhrwBusRHDmmQkH

Originally published at blog.pritishvaidya.com on March 2, 2019.