|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { View, Text, StyleSheet } from 'react-native'; |
| 3 | +import Animated, { |
| 4 | + useSharedValue, |
| 5 | + useAnimatedStyle, |
| 6 | + withTiming, |
| 7 | + runOnJS, |
| 8 | +} from 'react-native-reanimated'; |
| 9 | +import { |
| 10 | + NativeDetector, |
| 11 | + useExclusive, |
| 12 | + useLongPress, |
| 13 | + usePinch, |
| 14 | + useRotation, |
| 15 | + useSimultaneous, |
| 16 | + useTap, |
| 17 | +} from 'react-native-gesture-handler'; |
| 18 | + |
| 19 | +export default function Lock() { |
| 20 | + const rotation = useSharedValue(-Math.PI / 2); |
| 21 | + const savedRotation = useSharedValue(-Math.PI / 2); |
| 22 | + |
| 23 | + const scale = useSharedValue(0.6); |
| 24 | + const savedScale = useSharedValue(1); |
| 25 | + |
| 26 | + const [locked, setLocked] = useState(true); |
| 27 | + const snapThreshold = 0.4; |
| 28 | + const scaleThreshold = 0.1; |
| 29 | + const minScale = 0.5; |
| 30 | + const maxScale = 1; |
| 31 | + const TWO_PI = 2 * Math.PI; |
| 32 | + |
| 33 | + // Tap to lock |
| 34 | + const tap = useTap({ |
| 35 | + onEnd: () => { |
| 36 | + 'worklet'; |
| 37 | + if (savedRotation.value === 0 && scale.value === maxScale) { |
| 38 | + runOnJS(setLocked)(false); |
| 39 | + } |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + // Long press to cancel tap |
| 44 | + const longPress = useLongPress({}); |
| 45 | + |
| 46 | + const confirm = useExclusive(longPress, tap); |
| 47 | + |
| 48 | + const rotationGesture = useRotation({ |
| 49 | + onUpdate: (e) => { |
| 50 | + 'worklet'; |
| 51 | + rotation.value = savedRotation.value + e.handlerData.rotation; |
| 52 | + |
| 53 | + if (!locked) { |
| 54 | + runOnJS(setLocked)(true); |
| 55 | + } |
| 56 | + }, |
| 57 | + onEnd: () => { |
| 58 | + 'worklet'; |
| 59 | + |
| 60 | + const nearestMultiple = Math.round(rotation.value / TWO_PI) * TWO_PI; |
| 61 | + |
| 62 | + if (Math.abs(rotation.value - nearestMultiple) < snapThreshold) { |
| 63 | + rotation.value = withTiming(nearestMultiple, { duration: 300 }); |
| 64 | + savedRotation.value = 0; |
| 65 | + } else { |
| 66 | + rotation.value = withTiming(-Math.PI / 2, { duration: 300 }); |
| 67 | + savedRotation.value = -Math.PI / 2; |
| 68 | + } |
| 69 | + }, |
| 70 | + simultaneousWithExternalGesture: confirm, |
| 71 | + }); |
| 72 | + |
| 73 | + const pinchGesture = usePinch({ |
| 74 | + onUpdate: (e) => { |
| 75 | + 'worklet'; |
| 76 | + const value = savedScale.value * e.handlerData.scale; |
| 77 | + if (value < minScale || value > maxScale) { |
| 78 | + return; |
| 79 | + } |
| 80 | + scale.value = value; |
| 81 | + |
| 82 | + if (!locked) { |
| 83 | + runOnJS(setLocked)(true); |
| 84 | + } |
| 85 | + }, |
| 86 | + onEnd: () => { |
| 87 | + 'worklet'; |
| 88 | + |
| 89 | + if (Math.abs(scale.value - maxScale) < scaleThreshold) { |
| 90 | + scale.value = withTiming(maxScale, { duration: 300 }); |
| 91 | + } else { |
| 92 | + scale.value = withTiming(minScale, { duration: 300 }); |
| 93 | + } |
| 94 | + savedScale.value = scale.value; |
| 95 | + }, |
| 96 | + simultaneousWithExternalGesture: confirm, |
| 97 | + }); |
| 98 | + |
| 99 | + const unlockingGesture = useSimultaneous(rotationGesture, pinchGesture); |
| 100 | + |
| 101 | + const animatedStyle = useAnimatedStyle(() => ({ |
| 102 | + transform: [ |
| 103 | + { rotateZ: `${(rotation.value / Math.PI) * 180}deg` }, |
| 104 | + { scale: scale.value }, |
| 105 | + ], |
| 106 | + })); |
| 107 | + |
| 108 | + return ( |
| 109 | + <View style={styles.container}> |
| 110 | + <View style={styles.outerBox}> |
| 111 | + <NativeDetector gesture={unlockingGesture}> |
| 112 | + <Animated.View style={[styles.box, animatedStyle]}> |
| 113 | + <NativeDetector gesture={confirm}> |
| 114 | + <Text style={styles.lockIcon}>{locked ? '🔒' : '🔓'}</Text> |
| 115 | + </NativeDetector> |
| 116 | + </Animated.View> |
| 117 | + </NativeDetector> |
| 118 | + </View> |
| 119 | + <Text>{locked ? 'Locked' : 'Unlocked!'}</Text> |
| 120 | + <Text style={styles.instructions}> |
| 121 | + Tou unlock rotate 90 degrees clockwise, and scale to fill the square. |
| 122 | + Then tap to confirm, longpress to cancel the tap |
| 123 | + </Text> |
| 124 | + </View> |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +const styles = StyleSheet.create({ |
| 129 | + container: { |
| 130 | + flex: 1, |
| 131 | + alignItems: 'center', |
| 132 | + justifyContent: 'center', |
| 133 | + }, |
| 134 | + lockIcon: { |
| 135 | + fontSize: 40, |
| 136 | + color: '#fff', |
| 137 | + fontWeight: 'bold', |
| 138 | + }, |
| 139 | + instructions: { |
| 140 | + color: '#9CA3AF', |
| 141 | + marginTop: 8, |
| 142 | + textAlign: 'center', |
| 143 | + paddingHorizontal: 16, |
| 144 | + }, |
| 145 | + |
| 146 | + outerBox: { |
| 147 | + height: 200, |
| 148 | + width: 200, |
| 149 | + backgroundColor: 'gray', |
| 150 | + borderRadius: 20, |
| 151 | + alignItems: 'center', |
| 152 | + justifyContent: 'center', |
| 153 | + marginBottom: 50, |
| 154 | + }, |
| 155 | + box: { |
| 156 | + height: 200, |
| 157 | + width: 200, |
| 158 | + backgroundColor: '#b58df1', |
| 159 | + borderRadius: 20, |
| 160 | + alignItems: 'center', |
| 161 | + justifyContent: 'center', |
| 162 | + }, |
| 163 | +}); |
0 commit comments