Support multi-touch behaviour with panning and scrolling
Maintainer thường phản hồi trong vòng 2 ngày
@latekvo đang làm issue này rồi.
Từ ngày 15/10/2024.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Description
I’m trying to implement something similar to the gesture behaviour on the default list view on iOS. Eg. the “Reminders” app inside iOS.
It consist of a scrollable view with some list item's inside. When you long-press a list item, it lets you rearrange where the item belong. At the surface, this implementation seems quite simple - a ScrollView wrapping around multiple ListItem component's that is using the Gesture.Pan().activateAfterLongPress(250).
The code could look something like this
import { useRef } from "react";
import { Dimensions, View } from "react-native";
import {
Gesture,
GestureDetector,
ScrollView,
} from "react-native-gesture-handler";
import Animated, {
useAnimatedStyle,
useSharedValue,
withSpring,
} from "react-native-reanimated";
import { Text } from "../restyled/Text";
const items = [
{ id: "id_John", name: "John" },
{ id: "id_Jane", name: "Jane" },
{ id: "id_Jack", name: "Jack" },
{ id: "id_Jill", name: "Jill" },
{ id: "id_Joe", name: "Joe" },
{ id: "id_Jim", name: "Jim" },
{ id: "id_Joy", name: "Joy" },
];
const listItemHeight = 75;
const listItemWidth = Dimensions.get("window").width;
type Position = { x: number; y: number };
export function List() {
return (
<ScrollView style={{ flex: 1, gap: 12 }}>
{items.map((item, index) => (
<ListItem key={item.id} id={item.id} />
))}
</ScrollView>
);
}
type ListItemProps = {
id: string;
};
function ListItem(props: ListItemProps) {
const { id } = props;
const sharedSelectedItem = useSharedValue<string | null>(null);
const sharedSelectedPosStart = useRef(
useSharedValue<Position>({
x: 0,
y: 0,
})
).current;
const sharedSelectedPos = useRef(
useSharedValue<Position>({
x: 0,
y: 0,
})
).current;
const panGesture = Gesture.Pan()
.activateAfterLongPress(250)
.onStart((e) => {
sharedSelectedItem.value = id;
sharedSelectedPosStart.value = { x: e.absoluteX, y: e.absoluteY };
})
.onUpdate((e) => {
sharedSelectedPos.value = {
y: e.absoluteY - sharedSelectedPosStart.value.y,
x: e.absoluteX - sharedSelectedPosStart.value.x,
};
})
.onFinalize((e) => {
sharedSelectedItem.value = null;
sharedSelectedPos.value = {
x: withSpring(0),
y: withSpring(0),
};
});
const animatedStyle = useAnimatedStyle(() => {
const active = sharedSelectedItem.value === id;
return {
backgroundColor: active ? "blue" : "gray",
opacity: active ? 1 : 0.5,
transform: [
{
translateX: sharedSelectedPos.value.x,
},
{
translateY: sharedSelectedPos.value.y,
},
],
};
});
return (
<GestureDetector gesture={panGesture}>
<Animated.View
key={props.id}
style={[
{
height: listItemHeight,
width: listItemWidth,
justifyContent: "center",
},
animatedStyle,
]}
>
<Text style={{ textAlign: "center" }}>{props.id}</Text>
</Animated.View>
</GestureDetector>
);
}
The problem arise when the scrollable list is expanding beyond the device height dimensions. Because if you would like to drag a ListItem to the bottom of the list, then you need to be able to support scrolling while panning (dragging the item around). To support this, iOS implement 2 different scroll behaviors.
- Hot areas in top and bottom of the screen, that imperatively scroll at a constant pace.
- Support for scroll with a secondary finger, while the panning is active with the primary finger.
I think that bullet 1. is possible by keeping track of the fingers absolute position on the screen, and activate scroll imperatively, when the primary finger is entering hot areas in the top or bottom of the screen.
But bullet 2. doesn't seem to be possible by the primitive provider by RNGH currently.
By default, ScrollView's seems to be dismissed when a pan-gesture is active. The simultaneousHandlers prop allow the ScrollView gesture, and Gesture.Pan, to be active simultaneously - but this let the primary finger, pan and scroll at the same time.
What seems to be the crux of the problem, is that when a Gesture.Pan is active, RNGH is disabling ScrollView gestures globally, and not locally to the current gesture in action. If theGesture.Pan instead recognized and ignored the scrollview events, for the local pan-gesture in action, it would allow the secondary finger to scroll. What seems to be missing is a way to distingquish between disabling and ignoring ScrollView events for a given gesture.
The multi-touch behavior combining pan with the primary finger, and scrolling with secondary finger, is used multiple places inside iOS. E.g when.
- rearranging widgets on widget screen (vertical scroll)
- rearranging icons on one of the homescreen (horizontal scroll)
- rearranging items on a native list view (vertical scroll)
I noticed that Gesture.Native is able to wrap and listen to events on the ScrollView, but I'm unable to figure out how this can be composed with behavior im describing above.
Steps to reproduce
The code could look something like this
import { useRef } from "react";
import { Dimensions, View } from "react-native";
import {
Gesture,
GestureDetector,
ScrollView,
} from "react-native-gesture-handler";
import Animated, {
useAnimatedStyle,
useSharedValue,
withSpring,
} from "react-native-reanimated";
import { Text } from "../restyled/Text";
const items = [
{ id: "id_John", name: "John" },
{ id: "id_Jane", name: "Jane" },
{ id: "id_Jack", name: "Jack" },
{ id: "id_Jill", name: "Jill" },
{ id: "id_Joe", name: "Joe" },
{ id: "id_Jim", name: "Jim" },
{ id: "id_Joy", name: "Joy" },
];
const listItemHeight = 75;
const listItemWidth = Dimensions.get("window").width;
type Position = { x: number; y: number };
export function List() {
return (
<ScrollView contentContainerStyle={{ flex: 1 }}>
<View style={{ gap: 12 }}>
{items.map((item, index) => (
<ListItem key={item.id} id={item.id} />
))}
</View>
</ScrollView>
);
}
type ListItemProps = {
id: string;
};
function ListItem(props: ListItemProps) {
const { id } = props;
const sharedSelectedItem = useSharedValue<string | null>(null);
const sharedSelectedPosStart = useRef(
useSharedValue<Position>({
x: 0,
y: 0,
})
).current;
const sharedSelectedPos = useRef(
useSharedValue<Position>({
x: 0,
y: 0,
})
).current;
const panGesture = Gesture.Pan()
.activateAfterLongPress(250)
.onStart((e) => {
sharedSelectedItem.value = id;
sharedSelectedPosStart.value = { x: e.absoluteX, y: e.absoluteY };
})
.onUpdate((e) => {
sharedSelectedPos.value = {
y: e.absoluteY - sharedSelectedPosStart.value.y,
x: e.absoluteX - sharedSelectedPosStart.value.x,
};
})
.onFinalize((e) => {
sharedSelectedItem.value = null;
sharedSelectedPos.value = {
x: withSpring(0),
y: withSpring(0),
};
});
const animatedStyle = useAnimatedStyle(() => {
const active = sharedSelectedItem.value === id;
return {
backgroundColor: active ? "blue" : "gray",
opacity: active ? 1 : 0.5,
transform: [
{
translateX: sharedSelectedPos.value.x,
},
{
translateY: sharedSelectedPos.value.y,
},
],
};
});
return (
<GestureDetector gesture={panGesture}>
<Animated.View
key={props.id}
style={[
{
height: listItemHeight,
width: listItemWidth,
justifyContent: "center",
},
animatedStyle,
]}
>
<Text style={{ textAlign: "center" }}>{props.id}</Text>
</Animated.View>
</GestureDetector>
);
}
Snack or a link to a repository
https://snack.expo.dev/@matis/99d863
Gesture Handler version
2.12.0
React Native version
0.72.4
Platforms
Android, iOS
JavaScript runtime
None
Workflow
None
Architecture
None
Build type
None
Device
None
Device model
No response
Acknowledgements
Yes
- Ngôn ngữ chính
- TypeScript
- Star
- 6.8k
- Fork
- 1.1k
- Merge trung bình
- 2 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 28
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Không có hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của software-mansion/react-native-gesture-handler
-
GestureDetector should bubble touches if all gestures fail (or some other generalized solution)Đang mởMissing repro Platform: Android Platform: iOS
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 39/100
software-mansion/react-native-gesture-handler#4714 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[Web] A pan that starts on a Pressable presses it when the mouse button is releasedCó thể đã có người làm @mokshupadhyay đã nhận 2 ngày trước. Đang mởPlatform: Web
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 57/100
software-mansion/react-native-gesture-handler#4713 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[iOS] Crash at app exit: static `_managers` map destroyed while `flushOperations` reads itCó thể đã có người làm @beqramo đã nhận 6 ngày trước. Đang mởMissing info Missing repro Platform: iOS Platform: MacOS
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 30/100
software-mansion/react-native-gesture-handler#4711 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[iOS][Fabric] Recognizer of a live handler stays on a recycled view and takes every drag on the component that reuses itCó thể đã có người làm @nihar777 đã nhận 6 ngày trước. Đang mởPlatform: iOS
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
software-mansion/react-native-gesture-handler#4710 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[iOS] Button press feedback invisible on quick taps when Reduce Motion is enabledCó thể đã có người làm @andreialecu đã nhận 8 ngày trước. Đang mởMissing info Platform: iOS Platform: MacOS
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
software-mansion/react-native-gesture-handler#4707 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của software-mansion/react-native-gesture-handler
Issue tương tự
-
agentic-workflows
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
opencollective/opencollective-frontend#12789 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Web download page shows a file as Completed when its download from S3 fails with an error responseĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
awslabs/visual-asset-management-system#2097 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
openwatersio/slackwater#591 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
compiler-explorer/compiler-explorer#9738 ·
Maintainer thường phản hồi trong vòng 2 ngày