2017-10-22 122 views
1

我有一個畫布大於手機屏幕大小的繪圖應用程序。我想實現用兩個手指滾動並用一個手指畫圖。到目前爲止,我可以使滾動工作很好,但是當涉及到繪圖時,該行開始,然後視圖中的圖形失去對觸摸的控制,從而只繪製第一部分。我認爲scrollview控制了。點可以畫得很好。使UIScrollView放棄UITouch

這是我的子類的UIScrollView

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    guard let touches = event?.touches(for: self) else { return } 
    if touches.count < 2 { 
     self.next?.touchesBegan(touches, with: event) 
    } else { 
     super.touchesBegan(touches, with: event) 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    guard let touches = event?.touches(for: self) else { return } 
    if touches.count < 2 { 
     self.next?.touchesEnded(touches, with: event) 
    } else { 
     super.touchesEnded(touches, with: event) 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    guard let touches = event?.touches(for: self) else { return } 
    if touches.count < 2 { 
     self.next?.touchesMoved(touches, with: event) 
    } else { 
     super.touchesMoved(touches, with: event) 
    } 
} 

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { 
    guard let touches = event?.touches(for: self) else { return } 
    if touches.count < 2 { 
     self.next?.touchesCancelled(touches, with: event) 
    } else { 
     super.touchesCancelled(touches, with: event) 
    } 
} 

override func touchesShouldCancel(in view: UIView) -> Bool { 
    if (type(of: view)) == UIScrollView.self { 
     return true 
    } 
    return false 
} 

回答

0

您需要連接到您的ScrollerView長按手勢識別器,與最短持續時間設置在0秒,也認識到只有1個觸摸取消觸摸在視圖選項處於活動狀態。

您可以在界面構建器上的屬性檢查器下找到所有這些選項。

請使用公差設置稍微調整以微調結果。

+0

我確實有**取消接觸在視圖選項活動**,但對不起,你是什麼意思只識別一次觸摸?我仍然希望滾動視圖能夠識別兩個觸摸,以及將一次觸摸傳遞給下一個響應者,這將是我的畫布。我會嘗試長按手勢識別器,但我需要經常更新畫布以模擬繪圖,並且我希望使用touchesmoved方法,因爲我不知道如何在識別器的選擇器中完成該操作,謝謝。答案順便會嘗試! –

+0

對不起,也許我不清楚。 您應該依靠GestureRecognizer捕捉觸摸並採取相應的行動,從而有效地取消觸摸底層視圖。 我成功使用此設置捕捉繪圖和手勢。 從我所瞭解的情況來看,UIScrollView無法有效地管理平移/拖拽手勢,將它們傳遞給它所包含的視圖。 –

+0

你能使它工作嗎? –