Skip to content

Commit

Permalink
ui: Add tap gesture
Browse files Browse the repository at this point in the history
- Note: There's a bug with the Reanimiated lib; applying fix found at
  software-mansion/react-native-reanimated#3355 (comment)
  • Loading branch information
letam committed Jun 12, 2023
1 parent 3205035 commit 5c37c46
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import EmojiSticker from "./components/EmojiSticker";

const PlaceholderImage = require("./assets/images/background-image.png");

// FIXME need reanimated update, see https://github.com/software-mansion/react-native-reanimated/issues/3355
if (typeof window !== "undefined" && !window._frameTimestamp) {
window._frameTimestamp = null;
}

export default function App() {
const [showAppOptions, setShowAppOptions] = useState(false);
const [selectedImage, setSelectedImage] = useState(null);
Expand Down
36 changes: 31 additions & 5 deletions components/EmojiSticker.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import { View, Image } from "react-native";
import { TapGestureHandler } from "react-native-gesture-handler";
import Animated, {
useAnimatedGestureHandler,
useAnimatedStyle,
useSharedValue,
withSpring,
} from "react-native-reanimated";

const AnimatedImage = Animated.createAnimatedComponent(Image);

export default function EmojiSticker({ imageSize, stickerSource }) {
const scaleImage = useSharedValue(imageSize);
const imageStyle = useAnimatedStyle(() => {
return {
width: withSpring(scaleImage.value),
height: withSpring(scaleImage.value),
};
});
const onDoubleTap = useAnimatedGestureHandler({
onActive: () => {
if (scaleImage.value !== imageSize * 2) {
scaleImage.value = scaleImage.value * 2;
}
},
});

return (
<View style={{ top: -350 }}>
<Image
source={stickerSource}
resizeMode="contain"
style={{ width: imageSize, height: imageSize }}
/>
<TapGestureHandler onGestureEvent={onDoubleTap} numberOfTaps={2}>
<AnimatedImage
source={stickerSource}
resizeMode="contain"
style={[imageStyle, { width: imageSize, height: imageSize }]}
/>
</TapGestureHandler>
</View>
);
}

0 comments on commit 5c37c46

Please sign in to comment.