Skip to content

Commit a2ab9db

Browse files
committed
close image with a close icon
1 parent 637f9c9 commit a2ab9db

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

src/HomeScreen.js

+56-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
Dimensions,
66
ImageBackground,
77
TouchableWithoutFeedback,
8-
Animated
8+
Animated,
9+
Image
910
} from 'react-native';
1011
import { Text } from 'react-native-ui-kitten'
1112
import { SafeAreaView } from 'react-navigation'
@@ -30,7 +31,8 @@ class HomeScreen extends Component {
3031
{ title: "Breaking Bad", imageUrl: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
3132
{ title: "Breaking Bad", imageUrl: "https://www.thetvdb.com/banners/_cache/fanart/original/81189-1.jpg" },
3233
],
33-
activeImage: null
34+
activeImage: null,
35+
showCloseIcon: false
3436
}
3537

3638
componentWillMount() {
@@ -40,7 +42,6 @@ class HomeScreen extends Component {
4042
this.dimensions = new Animated.ValueXY()
4143
}
4244

43-
4445
_openImage = (index) => {
4546
const ref = this.allImagesRefs[index]
4647
ref.measure((x, y, width, height, pageX, pageY) => {
@@ -81,7 +82,9 @@ class HomeScreen extends Component {
8182
toValue: dHeight,
8283
duration: 300
8384
})
84-
]).start()
85+
]).start(() => {
86+
this.setState({ showCloseIcon: true })
87+
})
8588
})
8689
})
8790
})
@@ -113,7 +116,46 @@ class HomeScreen extends Component {
113116
)
114117
}
115118

119+
_closeImage = () => {
120+
this.setState({ showCloseIcon: false })
121+
Animated.parallel([
122+
Animated.timing(this.position.x, {
123+
toValue: this.oldPosition.x,
124+
duration: 300
125+
}),
126+
Animated.timing(this.position.y, {
127+
toValue: this.oldPosition.y,
128+
duration: 300
129+
}),
130+
Animated.timing(this.dimensions.x, {
131+
toValue: this.oldPosition.width,
132+
duration: 300
133+
}),
134+
Animated.timing(this.dimensions.y, {
135+
toValue: this.oldPosition.height,
136+
duration: 300
137+
})
138+
]).start(() => {
139+
this.setState({ activeImage: null })
140+
})
141+
}
142+
143+
_showCloseIcon() {
144+
if (this.state.showCloseIcon) {
145+
return <TouchableWithoutFeedback
146+
onPress={this._closeImage}
147+
>
148+
<Image
149+
style={styles.closeIcon}
150+
source={require('./img/cancel.png')} />
151+
</TouchableWithoutFeedback>
152+
} else {
153+
return null
154+
}
155+
}
156+
116157
render() {
158+
// These params are manipulated by Animation
117159
const activeImageStyle = {
118160
width: this.dimensions.x,
119161
height: this.dimensions.y,
@@ -123,9 +165,6 @@ class HomeScreen extends Component {
123165

124166
return (
125167
<SafeAreaView style={styles.container}>
126-
<Text category="h4" style={styles.welcome}>Welcome to Home Screen</Text>
127-
<Text style={styles.instructions}>To get started, edit App.js</Text>
128-
129168
<FlatList
130169
data={this.state.images}
131170
renderItem={this._renderItem.bind(this)}
@@ -136,13 +175,14 @@ class HomeScreen extends Component {
136175
pointerEvents={this.state.activeImage ? 'auto' : 'none'}
137176
>
138177
<View
139-
style={{ flex: 2 }}
178+
style={{ flex: 2, marginBottom: 80 }}
140179
ref={view => (this.viewImage = view)}>
141180
<Animated.Image
142181
style={[{ top: 0, left: 0, height: null, width: null, resizeMode: 'cover' }, activeImageStyle]}
143182
source={{ uri: this.state.activeImage ? this.state.activeImage.imageUrl : null }}
144183
>
145184
</Animated.Image>
185+
{this._showCloseIcon()}
146186
</View>
147187
</View>
148188
</SafeAreaView>
@@ -178,14 +218,12 @@ const styles = StyleSheet.create({
178218
shadowColor: '#424242',
179219
shadowRadius: 8
180220
},
181-
welcome: {
182-
fontSize: 20,
183-
textAlign: 'center',
184-
margin: 10,
185-
},
186-
instructions: {
187-
textAlign: 'center',
188-
color: '#333333',
189-
marginBottom: 5,
190-
},
221+
closeIcon: {
222+
position: 'absolute',
223+
right: 20,
224+
top: 40,
225+
height: 32,
226+
width: 32,
227+
tintColor: 'white'
228+
}
191229
});

src/img/cancel.png

4.43 KB
Loading

0 commit comments

Comments
 (0)