問題描述
我有兩個視圖控制器.視圖控制器 A 有一個 UIScrollView
并呈現視圖控制器 B.呈現是交互式的,由 scrollView.contentOffset
控制.
I have two view controllers. View controller A has a UIScrollView
and presents view controller B. The presentation is interactive and controlled by the scrollView.contentOffset
.
我想集成一個交互式關閉過渡:向上平移時,應以交互方式關閉 ViewController B.交互式關閉過渡還應控制 ViewController A scrollView.
I want to integrate an interactive dismiss transition: When panning up, ViewController B should be dismissed interactively. The interactive dismiss transition should also control the ViewController A scrollView.
我的第一次嘗試是使用 UIPanGestureRecognizer
并根據平移距離設置 scrollView.contentOffset
.這可行,但是當平移手勢結束時,滾動視圖偏移必須動畫到結束位置.使用 -[UIScrollView setContentOffset:animated:
不是一個好的解決方案,因為它使用線性動畫,沒有考慮當前的平移速度,也不能很好地減速.
My first attempt was using a UIPanGestureRecognizer
and setting the scrollView.contentOffset
according to the panned distance. This works, however when the pan gesture is ended, the scrollView offset has to be animated to the end position. Using -[UIScrollView setContentOffset:animated:
is not a good solution since it uses a linear animation, doesn't take the current pan velocity into account and doesn't decelerate nicely.
所以我認為應該可以將我的平移手勢識別器中的觸摸事件輸入到滾動視圖中.這應該給我們所有漂亮的滾動視圖動畫行為.
So I thought it should be possible to feed the touch events from my pan gesture recognizer into the scroll view. This should give us all the nice scroll view animation behavior.
我嘗試在我的 UIPanGestureRecognizer
子類中覆蓋 -touchesBegan/Moved/Ended/Cancelled withEvent:
方法,如下所示:
I tried overriding the -touchesBegan/Moved/Ended/Cancelled withEvent:
methods in my UIPanGestureRecognizer
subclass like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[scrollView touchesBegan:touches withEvent:event];
[scrollView.panGestureRecognizer touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
但顯然有些東西阻止了滾動視圖進入 tracking
模式.(確實是 dragging = YES
但僅此而已.)我驗證了 scrollView 是 userInteractionEnabled
,沒有隱藏并添加到視圖層次結構中.
But apparently something is blocking the scroll view from entering tracking
mode. (It does go to dragging = YES
but that's about it.) I verified the scrollView is userInteractionEnabled
, not hidden and added to the view hierarchy.
那么如何將我的觸摸事件轉發到 UIScrollView
?
So how can I forward my touch events to UIScrollView
?
推薦答案
讀完一個有趣的答案描述UIScrollView
的事件流,我得出的結論是,嘗試從手勢識別器遠程控制"滾動視圖可能很難實現,因為觸摸在路由到視圖和手勢識別器時會發生變化.由于 UITouch
不符合 NSCopying
我們也不能克隆觸摸事件以便稍后在未修改狀態下發送它們.
After reading an interesting answer describing UIScrollView
's event flow, I came to the conclusion that trying to "remote control" a scroll view from a gesture recognizer is probably very hard to achieve because touches are mutated while being routed to views and gesture recognizers. Since UITouch
doesn't conform to NSCopying
we also can't clone touch events in order to send them later in unmodified state.
雖然沒有真正解決我所要求的問題,但我找到了一種解決方法來完成我所需要的.我剛剛向視圖控制器 B 添加了一個滾動視圖,并將其與 VC A 的滾動視圖同步(垂直滾動時添加到視圖層次結構中):
While not really solving the problem I asked for, I found a workaround to accomplish what I need. I just added a scroll view to view controller B and synced it with VC A's scroll view (which is added to the view hierarchy when vertically scrolling):
// delegate of VC B's scrollView
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
scrollViewA.contentOffset = scrollView.contentOffset;
}
感謝 Friedrich Markgraf 提出這個想法.
Thanks to Friedrich Markgraf who came up with the idea.
這篇關于向前觸摸到 UIScrollView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!