<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Zohar Levin on Medium]]></title>
        <description><![CDATA[Stories by Zohar Levin on Medium]]></description>
        <link>https://medium.com/@zoharlevin_developer?source=rss-1e8fe4563508------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8WyXmBJdSafdSu4FH-7wLw.jpeg</url>
            <title>Stories by Zohar Levin on Medium</title>
            <link>https://medium.com/@zoharlevin_developer?source=rss-1e8fe4563508------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 18:53:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zoharlevin_developer/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How I linked animated headers to scroll position in React Native]]></title>
            <link>https://medium.com/dailyjs/how-i-linked-animated-headers-to-scroll-position-in-react-native-1a4906fca25b?source=rss-1e8fe4563508------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a4906fca25b</guid>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[flex]]></category>
            <dc:creator><![CDATA[Zohar Levin]]></dc:creator>
            <pubDate>Wed, 03 Jan 2018 09:31:59 GMT</pubDate>
            <atom:updated>2018-01-04T15:31:45.370Z</atom:updated>
            <content:encoded><![CDATA[<p>Here at <a href="https://www.solutotlv.com/">Soluto</a> we use React Native to build our mobile apps. We often use <a href="https://facebook.github.io/react-native/docs/animated.html">Animated</a> to drive animations designed to give our users a smoother and friendlier experience.</p><p>Recently, I was working on a feature that called for a cool, fun animated header. When researching online for how to get started I discovered that most information on the web about React Native <a href="https://facebook.github.io/react-native/docs/animated.html">Animated</a> refers to “independent” animations, the kind triggered by clicking a button or loading a page.</p><p>I couldn’t find a lot of info on <strong>animating components based on the scroll position of a ScrollView</strong>. So I’ve decided to share my two cents here in the hopes that it will make this job easier for the next dev to tackle something similar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*4877k4Hq9dPdtmvg9hnGFA.jpeg" /></figure><h3>How it looks</h3><p><a href="https://github.com/zoharlevin/react-native-animated-header-example">Here’s</a> an example I created that I’ll refer to throughout the post:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*xMZz_UGA94maiQeU.gif" /></figure><p>My example is composed of an Animated.ScrollView containing an AnimatedHeader component which is basically a View with different types of Animated components inside of it:</p><ul><li>Animated.Text (which reads “This is Animated Text”)</li><li>Animated.Image (the cute doggy).</li></ul><p>Each of the Animated components inside the header have styling/transforms/scaling/opacity linked to animationRangewhich is the current scroll position Animated.Value.</p><p>The scroll position of a scrollView can be mapped to an Animated.Value using Animated.event as explained <a href="https://facebook.github.io/react-native/docs/animated.html#handling-gestures-and-other-events">here</a> .</p><p>Basically, I’m linking a property in the state named scrollY to the value of the scroll position inside onScroll, here’s the code for the ScrollView and header:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9531da583aaa5b0bedd4848449d369a/href">https://medium.com/media/a9531da583aaa5b0bedd4848449d369a/href</a></iframe><p>The line nativeEvent: {contentOffset: {y: scrollY}} is equivalent to scrollY = e.nativeEvent.contentOffset.y.</p><p>After that I normalized the value and called it animationRange.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*4877k4Hq9dPdtmvg9hnGFA.jpeg" /></figure><h4>Now that we’ve covered the basics, here are 4 tips that I used in my example:</h4><h3>Always use Native Driver</h3><p>Always set useNativeDriver:true in your animation configurations. It will make your animations smooth by skipping the back-and-forth over the bridge between JS code and native code for every rendered frame.</p><p>I recommend further reading <a href="https://facebook.github.io/react-native/docs/animations.html#using-the-native-driver">here</a> .</p><p>You should, however, note the caveats of this flag. Though I believe they are well worth the smoothness achieved and we’ve made it a habit in <a href="https://www.solutotlv.com/">Soluto</a> of always setting useNativeDriver:true and working around whatever limitations it may bring. So far it&#39;s proven to be little pain for a lot of gain.</p><p>The source code for React Native contains the full list of white-listed animation styles supported with native driver, it can be found <a href="https://github.com/facebook/react-native/blob/dd5ac841d2d88d3d7c2270f835aa5d9f8bfac5c3/Libraries/Animated/src/NativeAnimatedHelper.js#L101">here</a>.</p><p>This feature is supported in react native version 0.40 and up.</p><h3>Use flexbox with onLayout</h3><p>When using flexbox you don’t know the absolute positions of components so at first glance it seems difficult to figure out the absolute distances and sizes you need for the animations.</p><p>By calling onLayout you can get your component&#39;s x,y position, as well as its width and height. That will allow you to use flexbox positioning as usual for the initial positioning of your component, but to use the exact (absolute) measurements of location and size to compute the distances and scaling necessary for your animation.</p><p>This method can also be used if you need an invisible (color:&#39;transparent&#39;) &quot;placeholder&quot; component for calculation purposes only (a scenario I&#39;ve encountered before but haven&#39;t covered in this example).</p><p>You may notice I’m using <a href="https://github.com/acdlite/recompose">recompose</a> a lot in my code. Check it out if you are unfamiliar with this library. It makes writing high order components much easier and more elegant. Of course everything I did with recompose can be done in other ways without HOC’s.</p><p>This is the code for the HOCs of self measurement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/373b740aa32b2424e830d7c5abf91eeb/href">https://medium.com/media/373b740aa32b2424e830d7c5abf91eeb/href</a></iframe><p>And here you can see how to use the self measurement HOCs together with onLayout in the AnimatedText component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/abffe11d1b0fb16b2384c749561b00a2/href">https://medium.com/media/abffe11d1b0fb16b2384c749561b00a2/href</a></iframe><h3>Avoid awkward mid-animation stops</h3><p>Always snap to the edges of the animation if the user stops scrolling in the middle.</p><p>This can be achieved by using onScrollEndDrag and onMomentumScrollEnd, as I did in the ScrollView component:</p><pre>const Demo = ({scrollY, animationRange}) =&gt; {  <br>    let _scrollView = null;<br><br>    const onScrollEndSnapToEdge = event =&gt; {<br>        const y = event.nativeEvent.contentOffset.y;<br>        if (0 &lt; y &amp;&amp; y &lt; scrollRangeForAnimation / 2) {<br>            if (_scrollView) {<br>                _scrollView.scrollTo({y: 0});<br>            }<br>        } else if (scrollRangeForAnimation / 2 &lt;= y &amp;&amp; y &lt; scrollRangeForAnimation) {<br>            if (_scrollView) {<br>                _scrollView.scrollTo({y: scrollRangeForAnimation});<br>            }<br>        }<br>    };<br><br>      return (<br>        &lt;View style={styles.container}&gt;                              <br>            &lt;Animated.ScrollView            <br>                style={styles.scrollView}<br>                ref={scrollView =&gt; {<br>                    _scrollView = scrollView ? scrollView._component : null;<br>                }}<br>                onScrollEndDrag={onScrollEndSnapToEdge}<br>                onMomentumScrollEnd={onScrollEndSnapToEdge}<br>                onScroll={Animated.event(<br>                    [<br>                        {<br>                            nativeEvent: {contentOffset: {y: scrollY}},<br>                        },<br>                    ],<br>                    {<br>                        useNativeDriver: true,<br>                    }<br>                )}</pre><h3>Decide on a scroll range for the animation</h3><p>Using a smaller or a larger scrolling range for your animation will make it feel longer or shorter. Try changing this number and see how it behaves.</p><p>Also, always normalize it for calculations. By interpolating the scroll range from whatever number you chose (such as [0,100] in my case) to [0,1] and using that in all of the internal components you will simplify your calculations in all the other components and avoid bugs.</p><p>In my example, you can see I set scrollRangeForAnimation:</p><pre>export const scrollRangeForAnimation = 100;</pre><p>And exposed it as animationRange to all other components:</p><pre>const enhance = compose(<br>    withState(&#39;scrollY&#39;, &#39;setScrollY&#39;, () =&gt; new Animated.Value(0)),<br>    withProps(({scrollY}) =&gt; ({<br>        animationRange: scrollY.interpolate({<br>            inputRange: [0, scrollRangeForAnimation],<br>            outputRange: [0, 1],<br>            extrapolate: &#39;clamp&#39;,<br>        }),<br>    }))<br>);<br><br>export default enhance(Demo);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZrJKJqBsksWd-8uKM9OvgA.png" /></figure><p>I hope you found this helpful. Please feel free to leave a comment or question!</p><blockquote><strong><em>The code for my example can be found </em></strong><a href="https://github.com/zoharlevin/react-native-animated-header-example"><strong><em>here</em></strong></a><strong><em>.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a4906fca25b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dailyjs/how-i-linked-animated-headers-to-scroll-position-in-react-native-1a4906fca25b">How I linked animated headers to scroll position in React Native</a> was originally published in <a href="https://medium.com/dailyjs">DailyJS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>