|
|
'use strict';
|
|
|
|
|
|
import React from 'react';
|
|
|
import ReactNative from 'react-native';
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
|
|
const {
|
|
|
View,
|
|
|
Image,
|
|
|
TouchableOpacity,
|
|
|
ListView,
|
|
|
ScrollView,
|
|
|
StyleSheet,
|
|
|
Dimensions,
|
|
|
Animated,
|
|
|
Text,
|
|
|
} = ReactNative;
|
|
|
|
|
|
export default class Section extends React.Component {
|
|
|
|
|
|
static propTypes = {
|
|
|
data: ImmutablePropTypes.listOf(
|
|
|
ImmutablePropTypes.contains({
|
|
|
image: React.PropTypes.string.isRequired,
|
|
|
url: React.PropTypes.string.isRequired,
|
|
|
})
|
|
|
),
|
|
|
width: React.PropTypes.number.isRequired,
|
|
|
height: React.PropTypes.number.isRequired,
|
|
|
onPress: React.PropTypes.func,
|
|
|
};
|
|
|
|
|
|
constructor(props) {
|
|
|
super (props);
|
|
|
|
|
|
this.animatedValue = new Animated.Value(Dimensions.get('window').width / 2);
|
|
|
|
|
|
this._renderRow = this._renderRow.bind(this);
|
|
|
this.state = {
|
|
|
offsetX: 0,
|
|
|
offsetXCenter: 0 + Dimensions.get('window').width / 2,
|
|
|
offsetXCenterAnim: new Animated.Value(Dimensions.get('window').width / 2),
|
|
|
};
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
_renderRow(item, i) {
|
|
|
return (
|
|
|
<TouchableOpacity
|
|
|
style={styles.page}
|
|
|
key={i}
|
|
|
>
|
|
|
<Animated.Image source={{uri: item.get('image')}} style={[styles.image]}/>
|
|
|
</TouchableOpacity>
|
|
|
);
|
|
|
}
|
|
|
|
|
|
render() {
|
|
|
let width = this.props.width;
|
|
|
let height = this.props.height;
|
|
|
let data = this.props.data.toArray();
|
|
|
|
|
|
return (
|
|
|
<View style={styles.container}>
|
|
|
<ScrollView
|
|
|
contentContainerStyle={styles.contentContainer}
|
|
|
horizontal={true}
|
|
|
pagingEnabled={false}
|
|
|
scrollEventThrottle={100}
|
|
|
bounces={false}
|
|
|
onScroll={(event) => {
|
|
|
// console.log(event.nativeEvent);
|
|
|
// this.offsetX = event.nativeEvent.contentOffset.x;
|
|
|
this.setState({
|
|
|
offsetX: event.nativeEvent.contentOffset.x,
|
|
|
offsetXCenter: event.nativeEvent.contentOffset.x + Dimensions.get('window').width / 2,
|
|
|
});
|
|
|
}}
|
|
|
onTouchEnd={(event) => {
|
|
|
// console.log('offsetY:', this.offsetY);
|
|
|
// console.log('touch info:', event.nativeEvent);
|
|
|
}}
|
|
|
>
|
|
|
{data.map((item, i) => {
|
|
|
let pageSize = 180;
|
|
|
let pageCenter = pageSize / 2 + i * pageSize;
|
|
|
let offsetXCenter = this.state.offsetXCenter;
|
|
|
// console.log(offsetXCenter);
|
|
|
// let distance = Math.abs(pageCenter - offsetXCenter);
|
|
|
let distance = offsetXCenter - pageCenter;
|
|
|
// i == 1 && console.log(distance);
|
|
|
distance = Math.abs(distance);
|
|
|
distance = distance > 180 ? 180 : distance;
|
|
|
// i == 1 && console.log(distance);
|
|
|
// distance = Math.round(distance * 100) / 100;
|
|
|
|
|
|
let scale = this.animatedValue.interpolate({
|
|
|
inputRange: [-200, -distance, -distance/2, 0, distance/2, distance, 200],
|
|
|
// inputRange: [-distance*2, -distance, -distance/2, 0, distance/2, distance, distance*2],
|
|
|
outputRange: [1, 0.65, 0.65, 0.85, 0.65, 0.65, 1],
|
|
|
});
|
|
|
// console.log(scale);
|
|
|
return (
|
|
|
<TouchableOpacity
|
|
|
style={styles.page}
|
|
|
key={i}
|
|
|
>
|
|
|
<Animated.Image source={{uri: item.get('image')}} style={[styles.image, {transform: [{ scale }]}]}/>
|
|
|
</TouchableOpacity>
|
|
|
);
|
|
|
})}
|
|
|
</ScrollView>
|
|
|
</View>
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
let styles = StyleSheet.create({
|
|
|
container: {
|
|
|
// flexDirection: 'row',
|
|
|
// height: 360,
|
|
|
// backgroundColor: 'green'
|
|
|
},
|
|
|
contentContainer: {
|
|
|
flexDirection: 'row',
|
|
|
// width: 220,
|
|
|
backgroundColor: 'gray'
|
|
|
},
|
|
|
page: {
|
|
|
width: 180,
|
|
|
// justifyContent: 'center',
|
|
|
paddingHorizontal: 15,
|
|
|
backgroundColor: 'gray',
|
|
|
|
|
|
},
|
|
|
image: {
|
|
|
width: 160,
|
|
|
height: 150,
|
|
|
// marginHorizontal: 15,
|
|
|
},
|
|
|
}); |