import React, { useRef, useState } from 'react';
import { View, Text, StyleSheet, Animated, TouchableOpacity, Image, ImageBackground, Easing } from 'react-native';
const WheelOfFortune = () => {
const rotationValue = useRef(new Animated.Value(0)).current;
const [clickCount, setClickCount] = useState(3);
const [rotationEnabled, setRotationEnabled] = useState(false);
const [resetAngle, setResetAngle] = useState(0);
const [startAngle, setStartAngle] = useState(0);
const rotateInterpolate = rotationValue.interpolate({
inputRange: [0, 1],
outputRange: [`${startAngle}deg`, `${startAngle + resetAngle}deg`],
});
const animatedStyle = {
transform: [{ rotate: rotateInterpolate }],
};
const startRotation = () => {
if (rotationEnabled) {
alert('禁止')
return
}
if (clickCount <= 0) {
alert('抽奖次数已用完')
return;
}
const animationConfig = {
toValue: 4,
duration: 10000,
easing: Easing.out(Easing.exp),
useNativeDriver: true,
};
setRotationEnabled(true);
Animated.timing(rotationValue, animationConfig).start(({ finished }) => {
if (finished) {
setClickCount(prevClickCount => prevClickCount - 1);
}
});
};
const resetRotation = () => {
setRotationEnabled(false);
setStartAngle(360); // 设置起始角度
setResetAngle(745); // 设置结束角度
// setStartAngle(Math.random() * 360); // 设置随机的起始角度
rotationValue.setValue(0);
};
const resetClickCount = () => {
setClickCount(3);
};
return (
<View style={styles.container}>
<ImageBackground
source={require('../../assets/imgs/luckyWheel/luckyWheel_bg.jpg')}
style={styles.backgroundImage}
>
<View style={{ marginVertical: 50 }}>
<Text style={styles.title}>幸运抽奖</Text>
<Text style={styles.cjcs}>剩余抽奖次数:{clickCount}</Text>
</View>
<TouchableOpacity onPress={() => resetRotation()}>
<View>
<Text style={styles.cjcs}>重置角度</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={resetClickCount}>
<View style={{ marginTop: 10 }}>
<Text style={styles.cjcs}>重置抽奖次数</Text>
</View>
</TouchableOpacity>
<View style={styles.box}>
<Animated.Image
source={require('../../assets/imgs/luckyWheel/img_bg.png')}
style={[styles.centerImage, animatedStyle]}
/>
<TouchableOpacity onPress={startRotation}>
<Image
source={require('../../assets/imgs/luckyWheel/img_pointer.png')}
style={styles.overlayImage}
/>
</TouchableOpacity>
</View>
</ImageBackground>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
backgroundImage: {
flex: 1,
width: '100%',
height: '100%',
resizeMode: 'cover',
position: 'absolute',
},
centerImage: {
width: 250,
height: 250,
resizeMode: 'contain',
marginTop: 100,
},
overlayImage: {
width: 100,
height: 100,
position: 'relative',
top: -190,
resizeMode: 'contain',
},
title: {
fontSize: 35,
fontWeight: 'bold',
color: 'white',
textAlign: 'center',
zIndex: 999,
},
cjcs: {
fontSize: 16,
fontWeight: 'bold',
color: 'white',
textAlign: 'center',
zIndex: 999,
},
box: {
height: 250,
width: 250,
justifyContent: 'center',
alignItems: 'center',
alignSelf: 'center',
marginTop: 50,
},
});
export default WheelOfFortune;
react native 大转盘抽奖
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 中国风水墨画背景PHP+Ajax幸运大转盘抽奖源码,通过奖品库存、中奖次数来计算中奖概率。恭喜你,中得五等奖,还要...
- 项目需求运用到大转盘 可设置概率 可直接自定义结果 效果如下 两种方法及结合法 一 通过canvas 实现 但...