Skip to content

Commit

Permalink
Adds TouchableDriver (#3)
Browse files Browse the repository at this point in the history
* Adds touchable driver component

* Adds touchable zoom in example

* Wrap text in quotes for better display in diff tools

* Extracts default animation out of class

* Removes animation override via constructor
  • Loading branch information
adri authored and SoHotSoup committed Oct 18, 2016
1 parent 78ea551 commit 2d91e17
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
62 changes: 62 additions & 0 deletions TouchableDriver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
Animated,
Easing,
} from 'react-native';

/**
* Returns the default animation callback to use.
*
* @param {Animated.Value} value
* @param {double} toValue
* @param {Object} animationOptions
* @returns {CompositeAnimation}
*/
function defaultAnimation(value, toValue, animationOptions) {
return Animated.timing(
value,
{
toValue: toValue,
...animationOptions
}
);
}

/**
* Animation driver that creates an animated value changed on press events.
*
* Assign onPressIn and onPressOut props of React Native Touchable*, and
* pass instance to any @shoutem/animation animation to run it
* e.g.:
* driver = new TouchableDriver();
* ...
* <TouchableOpacity {...driver.touchableViewProps}>
* ...
* <ZoomIn driver={driver}>
*/
export class TouchableDriver {

/**
* @param {Object} options Animation options.
*/
constructor(options) {
this.animationOptions = Object.assign({
easing: Easing.elastic(1),
duration: 150,
}, options);
this.value = new Animated.Value(0);
this.onPressIn = this.onPressIn.bind(this);
this.onPressOut = this.onPressOut.bind(this);
this.touchableViewProps = {
onPressIn: this.onPressIn,
onPressOut: this.onPressOut,
};
}

onPressIn() {
defaultAnimation(this.value, 1, this.animationOptions).start();
}

onPressOut() {
defaultAnimation(this.value, 0, this.animationOptions).start();
}
}
5 changes: 5 additions & 0 deletions examples/ShoutemAnimation/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import Parallax from './Parallax';
import HeroHeader from './HeroHeader';
import FadeInOut from './FadeInOut';
import ZoomInOut from './ZoomInOut';
import ZoomInTouchable from './ZoomInTouchable';

const examples = {
Parallax,
HeroHeader,
FadeInOut,
ZoomInOut,
ZoomInTouchable,
};

export class App extends Component {
Expand Down Expand Up @@ -46,6 +48,9 @@ export class App extends Component {
}, {
example: 'ZoomIn + ZoomOut (TimerDriver)',
component: 'ZoomInOut',
}, {
example: 'ZoomIn (TouchableDriver)',
component: 'ZoomInTouchable',
}]}
onOptionSelected={(option) => this.setState({ selectedComponent: option.component })}
titleProperty="example"
Expand Down
38 changes: 38 additions & 0 deletions examples/ShoutemAnimation/ZoomInTouchable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { Component } from 'react';
import { ScrollView } from 'react-native';

import {
ZoomIn,
TouchableDriver,
} from '@shoutem/animation';


import {
Button,
Row,
Icon,
Text,
View,
} from '@shoutem/ui';

export default class ZoomInTouchableExample extends Component {
render() {
const driver = new TouchableDriver();

return (
<View>
<Row>
<View styleName="horizontal h-center">
<ZoomIn driver={driver} maxFactor={1.3}>
<Button {...driver.touchableViewProps}>
<Icon name="add-to-favorites-full" />
<Text>{"TOUCH ME, I'M ZOOMING IN"}</Text>
</Button>
</ZoomIn>
</View>
</Row>
</View>
);
}
}

1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { default as makeZoomable } from './ZoomableComponent';
export { ScrollDriver } from './ScrollDriver';
export { TimingDriver } from './TimingDriver';
export { TouchableDriver } from './TouchableDriver';
export { Parallax } from './Parallax';
export { FadeIn } from './FadeIn';
export { FadeOut } from './FadeOut';
Expand Down

0 comments on commit 2d91e17

Please sign in to comment.