<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alexander Senichev</title>
    <description>The latest articles on DEV Community by Alexander Senichev (@a_senichev).</description>
    <link>https://dev.to/a_senichev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F185325%2F7fb0ffa9-3b13-40ab-a02f-f164a4acf371.jpg</url>
      <title>DEV Community: Alexander Senichev</title>
      <link>https://dev.to/a_senichev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/a_senichev"/>
    <language>en</language>
    <item>
      <title>Image scaling by the vertical scrolling bounce for iOS in React Native in 5 minutes.</title>
      <dc:creator>Alexander Senichev</dc:creator>
      <pubDate>Fri, 25 Dec 2020 23:03:46 +0000</pubDate>
      <link>https://dev.to/a_senichev/image-scaling-on-the-vertical-scrolling-bounce-for-ios-in-react-native-in-5-minutes-2c6</link>
      <guid>https://dev.to/a_senichev/image-scaling-on-the-vertical-scrolling-bounce-for-ios-in-react-native-in-5-minutes-2c6</guid>
      <description>&lt;p&gt;In this article, I'm going to demonstrate an easy approach how to build 60fps animation for a list header (e.g. image), when you are doing a bounce effect by scrolling up in the iOS application.&lt;/p&gt;

&lt;p&gt;I use &lt;code&gt;Expo workflow&lt;/code&gt; since it's easy to get started and &lt;code&gt;react-native-reanimated&lt;/code&gt; to make beauty and smooth animations:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install dependencies and start the project:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i expo-cli&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;expo init my-app&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;expo install react-native-reanimated&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Then create basic markup:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  return (
    &amp;lt;View style={styles.container}&amp;gt;
      &amp;lt;Image
        style={styles.image}
        source={{ uri: 'http://picsum.photos/1000/1000' }}
      /&amp;gt;
      &amp;lt;ScrollView contentContainerStyle={{ flexGrow: 1 }}&amp;gt;
        &amp;lt;View style={styles.items}&amp;gt;
          {new Array(15).fill(0).map((_, index) =&amp;gt; &amp;lt;View key={index} style={styles.item} /&amp;gt;)}
        &amp;lt;/View&amp;gt;
      &amp;lt;/ScrollView&amp;gt;
    &amp;lt;/View&amp;gt;
  );
}

const IMAGE_HEIGHT = 300

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#000',
  },
  items: {
    paddingTop: IMAGE_WIDTH,
    paddingHorizontal: 8,
  },
  image: {
    ...StyleSheet.absoluteFillObject,
    top: 0,
    height: IMAGE_WIDTH,
    width: '100%'
  },
  title: {
    color: '#FFF'
  },
  item: {
    backgroundColor: '#1C1C1C',
    height: 50,
    width: '100%',
    marginTop: 8,
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now let's do animation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As you can see we use &lt;code&gt;ScrollView&lt;/code&gt; and &lt;code&gt;Image&lt;/code&gt; from &lt;code&gt;react-native-reanimated&lt;/code&gt; to get animated nodes. &lt;/li&gt;
&lt;li&gt;Then we set up the throttle value to &lt;code&gt;16&lt;/code&gt;  for the &lt;code&gt;ScrollView&lt;/code&gt; node to throttle invocation of events. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useValue&lt;/code&gt; - hook to create &lt;code&gt;Animated.Value&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Animated.event&lt;/code&gt; takes an array of mappings and extracts values from each arg accordingly. In our case, we extract &lt;code&gt;contentOffset.y&lt;/code&gt; and assign it to &lt;code&gt;scrollY&lt;/code&gt;, so by native side value of &lt;code&gt;scrollY&lt;/code&gt; is always the same as &lt;code&gt;offsetY&lt;/code&gt; of our &lt;code&gt;Animated.ScrollView&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Interpolations of the &lt;code&gt;offsetY&lt;/code&gt; of &lt;code&gt;Animated.ScrollView&lt;/code&gt; for &lt;code&gt;Animated.Image&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;translateY&lt;/code&gt; is needed to translate the image to the top when we are scrolling down&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;scale&lt;/code&gt; just nicely scales within &lt;code&gt;outputRange&lt;/code&gt; the image by &lt;code&gt;inputRange&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;paddingTop&lt;/code&gt; for the content.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  const scrollY = useValue(0)
  return (
    &amp;lt;View style={styles.container}&amp;gt;
      &amp;lt;Animated.Image
        style={{
          ...styles.image,
          transform: [
            {
              translateY: interpolate(scrollY, {
                inputRange: [0, IMAGE_WIDTH],
                outputRange: [0, -IMAGE_WIDTH],
                extrapolate: Extrapolate.CLAMP,
              }),
              scale: interpolate(scrollY, {
                inputRange: [-IMAGE_WIDTH * 2, 0],
                outputRange: [5, 1],
                extrapolate: Extrapolate.CLAMP,
              }),
            },
          ],
        }}
        source={{ uri: 'http://picsum.photos/1000/1000' }}
      /&amp;gt;
      &amp;lt;Animated.ScrollView
        onScroll={Animated.event(
          [{ nativeEvent: { contentOffset: { y: scrollY } } }],
          { useNativeDriver: true }
        )}
        contentContainerStyle={{ flexGrow: 1 }}
        scrollEventThrottle={16}
      &amp;gt;
        &amp;lt;View style={styles.items}&amp;gt;
          {new Array(15).fill(0).map((_, index) =&amp;gt; &amp;lt;View key={index} style={styles.item} /&amp;gt;)}
        &amp;lt;/View&amp;gt;
      &amp;lt;/Animated.ScrollView&amp;gt;
    &amp;lt;/View&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/8aoDLflQNlU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;That's all, hope you are well. Happy coding :)&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
