問題描述
只是在玩 SDK,我想知道 UITouch 事件是否可以在 UIScrollView 中工作.
Just toying with the SDK and I was wondering if possible a UITouch event can work inside a UIScrollView.
我已經設置了一個處理大型 UIView 的 UIScrollView,在 UIView 內部是一個 UIImageView,我設法讓 UITouch 將 UIImageView 拖到 UIScrollView 之外,但在它內部沒有注冊事件.
I have setup a UIScrollView which handles a large UIView, inside the UIView is a UIImageView, I've managed to get the UITouch to drag the UIImageView outside of the UIScrollView but inside it's not registering the event.
我想我想要完成的是在大 UIView 周圍拖動 UIImageView 而 UIScrollView 沿著圖像移動,如果用戶將其拖動到 UIImageView 開始拖動時的 POS 之外,如果這有意義嗎?
I suppose what I was trying to accomplish was dragging the UIImageView around the large UIView whilst the UIScrollView moves along the image if the user drags it beyond the POS of when the UIView when the UIImageView began it's dragging, if that makes sense?
非常感謝
推薦答案
(如果我理解正確的話)
(If I understood the question correctly)
UIScrollView 攔截 touchMoved
事件,如果啟用滾動,則不會將它們傳播到其內容.因此,為了在我的應用程序中拖動 UIScrollView 內容,我做了以下技巧:
UIScrollView intercepts touchMoved
events and does not propagate them to its contents if scrolling is enabled. So to do the dragging in the UIScrollView contents in my app I did the following trick:
touchesBegan:檢查您是否觸摸了可拖動"區域.如果是 - 禁用 UIScrollView 中的滾動.
touchesBegan: Check if you touch the "draggable" region. If YES - disable scrolling in UIScrollView.
touchesMoved:現在,當滾動被禁用時,您的內容視圖會收到此事件,您可以相應地移動可拖動的 UIImageView.
touchesMoved: Now as scrolling is disabled your contents view receives this event and you can move your draggable UIImageView accordingly.
touchesEnded:在 UIScrolliew 中重新啟用滾動.
touchesEnded: Reenable scrolling in UIScrolliew.
如果您想將視圖拖到可見的 UIScrollView 區域之外,您可能還需要手動檢查您是否靠近邊界并手動調整內容偏移量(我自己沒有嘗試過,但我認為它應該可以工作).
If you want to drag the view outside of the visible UIScrollView area you may also need to check manually if you're near the boundaries and manually adjust contents offset (I didn't try this myself but I think it should work).
這篇關于在 UIScrollView 中使用 UITouch的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!