[英]How do you get the location of touches for a UILongPressGestureRecognizer when the number of required touches is more than one in swift?
[英]Swift: How do you get the velocity for each touches with UIPanGestureRecognizer?
我想使用UIPanGestureRecognizer
獲取每次觸摸的速度,就像您可以使用方法location(ofTouch: Int, in: UIView)
觸摸location(ofTouch: Int, in: UIView)
。 但是,當我使用方法velocity(in: view)
,它只返回觸摸的速度之一。 我嘗試更改maximumNumberOfTouches
屬性,但沒有用。
有什么辦法可以實現這一目標嗎?
好了,我通過創建一個二維數組的解決了這個CGPoint
稱為touchLocations
和的另一個陣列CGPoint
稱為touchVelocities
。 在循環遍歷所有觸摸的 for 循環中,我添加了
if !touchLocations.indices.contains(touchNumber) {
touchLocations.append([CGPoint]())
}
touchLocations[touchNumber].append(sender.location(ofTouch: touchNumber, in: view))
為每次觸摸分配一個新的CGPoint
( touchNumber
是觸摸的索引, sender
是手勢識別器)。
然后我加了
touchLocations[touchNumber] = touchLocations[touchNumber].suffix(2)
這樣數組只包含最后 2 個元素。
為了獲得速度,我只是做了
touchVelocities.insert(CGPoint(x: touchLocations[touchNumber].last!.x - touchLocations[touchNumber].first!.x, y: touchLocations[touchNumber].last!.y - touchLocations[touchNumber].first!.y), at: touchNumber)
(從 x 速度的第二個 x 值中減去第一個 x 值,並為 y 速度做同樣的事情)
我知道這種方法不是很准確,但是對於我的目的來說已經足夠了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.