作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前我在带有 Customscrollview 的列中使用 photoview(类似于 flutter 的交互式查看器)小部件。所以基本上我有一个应用程序链接 instagram 提要。然而,捏合缩放功能有一点问题,因为捏合手势有时会被 customscrollview 用于滚动。
所以我需要的是,每当有“多点触控”时,我需要 PhotoViewGestureRecognizer 来赢得 VerticalDragGestureRecognizer 的竞技场。
这是我的手势调试日志:
I/flutter (12956): Gesture arena 12 ❙ Adding: PhotoViewGestureRecognizer#6bdab(debugOwner: PhotoViewGestureDetector)
I/flutter (12956): Gesture arena 12 ❙ Adding: DoubleTapGestureRecognizer#6e985(debugOwner: PhotoViewGestureDetector)
I/flutter (12956): Gesture arena 12 ❙ Adding: VerticalDragGestureRecognizer#b7e88(start behavior: down)
I/flutter (12956): Gesture arena 12 ❙ Adding: HorizontalDragGestureRecognizer#d6b73(start behavior: down)
I/flutter (12956): Gesture arena 12 ❙ Closing with 4 members.
I/flutter (12956): Gesture arena 12 ❙ Rejecting: DoubleTapGestureRecognizer#6e985(debugOwner: PhotoViewGestureDetector)
I/flutter (12956): Gesture arena 11 ❙ Accepting: VerticalDragGestureRecognizer#b7e88(start behavior: down)
I/flutter (12956): Gesture arena 11 ❙ Self-declared winner: VerticalDragGestureRecognizer#b7e88(start behavior: down)
I/flutter (12956): Gesture arena 12 ❙ Accepting: VerticalDragGestureRecognizer#b7e88(start behavior: down)
I/flutter (12956): Gesture arena 12 ❙ Self-declared winner: VerticalDragGestureRecognizer#b7e88(start behavior: down)
最佳答案
我用这个类解决了这个问题
import 'package:flutter/gestures.dart';
class TouchCountRecognizer extends OneSequenceGestureRecognizer {
TouchCountRecognizer(this.onMultiTouchUpdated);
Function(bool) onMultiTouchUpdated;
int touchcount = 0;
@override
void addPointer(PointerDownEvent event) {
startTrackingPointer(event.pointer);
if (touchcount < 1) {
//resolve(GestureDisposition.rejected);
//_p = event.pointer;
onMultiTouchUpdated(false);
} else {
onMultiTouchUpdated(true);
//resolve(GestureDisposition.accepted);
}
touchcount++;
}
@override
String get debugDescription => 'touch count recognizer';
@override
void didStopTrackingLastPointer(int pointer) {}
@override
void handleEvent(PointerEvent event) {
if (!event.down) {
touchcount--;
if (touchcount < 1) {
onMultiTouchUpdated(false);
}
}
}
}
然后像这样包装您的自定义 ScrollView :
RawGestureDetector(
gestures: <Type, GestureRecognizerFactory>{
TouchCountRecognizer:
GestureRecognizerFactoryWithHandlers<TouchCountRecognizer>(
() => TouchCountRecognizer(this.onMultiTouchUpdated),
(TouchCountRecognizer instance) {},
),
},
child: NotificationListener(
// ignore: missing_return
onNotification: (notification) {
if (notification is ScrollStartNotification) {
_scrolling = true;
}
if (notification is ScrollUpdateNotification) {}
if (notification is ScrollEndNotification) {
_scrolling = false;
}
},
child: CustomScrollView(physics: _scrollPhysics,......)
然后像这样进行多点触控回调:
onMultiTouchUpdated(bool status) {
_multiTouch = status;
//setState(() {});
}
有条件地定义滚动物理:
if (!_scrolling && _multiTouch) {
_scrollPhysics = NeverScrollableScrollPhysics();
} else {
_scrollPhysics = AlwaysScrollableScrollPhysics();
}
基本上,我们正在尝试做的是检测用户是在尝试进行多点触控还是单点触控,并相应地优先选择捏合或滚动操作。
关于android - flutter : How to control conflicting gestures?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64034849/
我是一名优秀的程序员,十分优秀!