質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

Q&A

0回答

506閲覧

React Native、グッドボタンのサイズが右側だけに広がる

kontikuwa

総合スコア26

React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

0グッド

0クリップ

投稿2021/08/13 22:04

いいねボタンをReactNativeのAnimatedで実装しているのだが、下の画像の通り(わかりにくいかもしれません。)、アイコンのサイズを大きくしても、右にしか広がらず、いいねボタンを押したときに不自然?に右だけ広がるの直したいので、右方向だけでなく、両方にサイズが大きくなる方法?教えていただきたい。

イメージ説明イメージ説明

ReactNative

1import React, {useState, useRef, useEffect} from 'react'; 2import { 3 View, 4 Image, 5 Pressable, 6 StyleSheet, 7 FlatList, 8 Animated, 9 Alert, 10} from 'react-native'; 11import Icon2 from 'react-native-vector-icons/AntDesign'; 12import ReactNativeHaptic from 'react-native-haptic'; 13import ReadMore from '@fawazahmed/react-native-read-more'; 14 15const AnimatedIcon = Animated.createAnimatedComponent(Icon2); 16 17const Main = props => { 18 const [liked, setLiked] = useState(false); 19 const [data, setData] = useState(); 20 const [loading, setLoading] = useState(true); 21 22 // .currentに値を保持し、それを後で書き換えることでアニメーションになる。 23 const animatedValue = useRef(new Animated.Value(0)).current; 24 25 // 0 ~ 150 のフレーム間での「色」の設定 26 const interPolateColor = animatedValue.interpolate({ 27 inputRange: [0, 150], 28 outputRange: ['#262626', '#ed4956'], 29 }); 30 const animatedIconStyle = { 31 color: interPolateColor, 32 }; 33 34 // 0 ~ 150 のフレーム間での大きさの設定 35 const interPolateSize = animatedValue.interpolate({ 36 inputRange: [0, 50, 100, 150], 37 outputRange: [28, 38, 93, 28], 38 }); 39 const animatedIconSize = { 40 fontSize: interPolateSize, 41 }; 42 43 const loadData = () => { 44 fetch('http://127.0.0.1:8000/api/articles/', { 45 method:'GET', 46 }) 47 .then(resp => resp.json()) 48 .then(res => { 49 setData(res); 50 setLoading(false); 51 }) 52 .catch(error => Alert.alert('error', error)); 53 }; 54 55 useEffect(() => { 56 loadData(); 57 }, []); 58 59 const _onPress = () => { 60 if (liked === false) { 61 ReactNativeHaptic.generate('selection'); 62 // 第一引数に変化させたい要素?、第2引数にどのように変化させるのかを書く 63 Animated.timing(animatedValue, { 64 toValue: 150, 65 duration: 200, 66 }).start(); 67 setLiked((isLiked) => !isLiked); 68 } else { 69 Animated.timing(animatedValue, { 70 toValue: 0, 71 duration: 200, 72 }).start(); 73 setLiked((isLiked) => !isLiked); 74 } 75 }; 76 77 return ( 78 <View style={styles.main}> 79 <FlatList 80 onRefresh={() => loadData()} 81 refreshing={loading} 82 data={data} 83 numColumns={1} 84 renderItem={({item}) => ( 85 <View style={styles.contaier}> 86 <Image 87 style={styles.image} 88 source={{ 89 uri: item.picture, 90 }} 91 /> 92 <View style={styles.heart}> 93 <Pressable 94 onPress={_onPress}> 95 <Animated.Text style={animatedIconStyle}> 96 <AnimatedIcon 97 style={animatedIconSize} 98 name={ liked ? 'heart' : 'hearto'} 99 /> 100 </Animated.Text> 101 </Pressable> 102 </View> 103 <View 104 style={styles.maintext} 105 > 106 <ReadMore 107 numberOfLines={3} 108 seeMoreStyle={{color: '#8e8e8e'}} 109 seeLessStyle={{color: '#8e8e8e'}} 110 seeMoreText={'more'} 111 > 112 {item.description} 113 </ReadMore> 114 </View> 115 </View> 116 )} 117 /> 118 </View> 119 ); 120}; 121 122const styles = StyleSheet.create({ 123 main: { 124 flex: 1, 125 width: '100%', 126 backgroundColor: 'white', 127 }, 128 contaier: { 129 alignItems: 'center', 130 }, 131 image: { 132 width: '100%', 133 aspectRatio: 1, 134 }, 135 heart: { 136 paddingTop: '2%', 137 paddingBottom: '2%', 138 flexDirection: 'row', 139 alignItems: 'center', 140 width: '90%', 141 borderWidth: 1, 142 }, 143 maintext: { 144 width: '90%', 145 paddingBottom: '10%', 146 }, 147}); 148 149export default Main; 150

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問