檢測iphone上的特殊觸控

[英]Detecting Special touch on the iphone


I was asking myself if there are examples online which covers how you can for instance detect shapes in touch gestures.

我問自己是否有在線示例,其中包括如何在觸摸手勢中檢測形狀。

for example a rectangle or a circle (or more complex a heart .. )

例如一個矩形或一個圓形(或更復雜的一顆心......)

or determine the speed of swiping (over time ( like i'm swiping my iphone against 50mph ))

或者確定滑動的速度(隨着時間的推移(就像我將我的iphone刷到50英里每小時))

2 个解决方案

#1


For very simple gestures (horizontal vs. vertical swipe), calculate the difference in x and y between two touches.

對於非常簡單的手勢(水平與垂直滑動),計算兩次觸摸之間x和y的差異。

dy = abs(y2 - y1)
dx = abs(x2 - x1)
f = dy/dx

An f close to zero is a horizontal swipe. An f close to 1 is a diagonal swipe. And a very large f is a vertical swipe (keep in mind that dx could be zero, so the above won't yield valid results for all x and y).

接近零的f是水平滑動。接近1的f是對角線滑動。並且非常大的f是垂直滑動(請記住,dx可能為零,因此上述不會產生所有x和y的有效結果)。

If you're interested in speed, pythagoras can help. The length of the distance travelled between two touches is:

如果你對速度感興趣,畢達哥拉斯可以提供幫助。兩次觸摸之間的距離長度為:

l = sqrt(dx*dx + dy*dy)

If the touches happened at times t1 and t2, the speed is:

如果觸摸發生在時間t1和t2,則速度為:

tdiff = abs(t2 - t1)
s = l/tdiff

It's up to you to determine which value of s you interpret as fast or slow.

由您來決定您解釋為快或慢的s值。

You can extend this approach for more complex figures, e.g. your square shape could be a horizontal/vertical/horizontal/vertical swipe with start/end points where the previous swipe stopped.

您可以將此方法擴展到更復雜的數字,例如您的方形形狀可以是水平/垂直/水平/垂直滑動,其中前一個滑動停止的起點/終點。

For more complex figures, it's probably better to work with an idealized shape. One could consider a polygon shape as the ideal, and check if a range of touches

對於更復雜的數字,使用理想化的形狀可能更好。可以將多邊形形狀視為理想形狀,並檢查是否有一系列觸摸

  1. don't have too high a distance to their closest point on the pologyon's outline, and
  2. 距離pologyon輪廓上最近點的距離不是太遠

  3. all touches follow the same direction along the polygon's outline.
  4. 所有觸摸都沿着多邊形的輪廓沿着相同的方向。

You can refine things further from there.

你可以從那里進一步改進。

#2


There does exist other methods for detecting non-simple touches on a touchscreen. Check out the $1 unistroke gesture recognizer at the University of Washington. http://depts.washington.edu/aimgroup/proj/dollar/

存在用於檢測觸摸屏上的非簡單觸摸的其他方法。查看華盛頓大學的$ 1 unistroke手勢識別器。 http://depts.washington.edu/aimgroup/proj/dollar/

It basically works like this:

它基本上是這樣的:

  1. Resample the recorded path into a fixed number of points that are evenly spaced along the path
  2. 將記錄的路徑重新采樣為沿路徑均勻分布的固定數量的點

  3. Rotating the path so that the first point is directly to the right of the path’s center of mass
  4. 旋轉路徑,使第一個點直接位於路徑質心的右​​側

  5. Scaling the path (non-uniformly) to a fixed height and width
  6. 將路徑(非均勻地)縮放到固定的高度和寬度

  7. For each reference path, calculating the average distance for the corresponding points in the input path. The path with the lowest average point distance is the match.
  8. 對於每個參考路徑,計算輸入路徑中相應點的平均距離。平均點距離最小的路徑是匹配。

What’s great is that the output of steps 1-3 is a reference path that can be added to the array of known gestures. This makes it extremely easy to give your application gesture support and create your own set of custom gestures, as you see fit.

最棒的是步驟1-3的輸出是可以添加到已知手勢陣列的參考路徑。這使您可以非常輕松地提供應用程序手勢支持,並根據需要創建自己的一組自定義手勢。

This has been ported to iOS by Adam Preble, repo on github: http://github.com/preble/GLGestureRecognizer

這已由Adam Preble移植到iOS,github上的repo:http://github.com/preble/GLGestureRecognizer


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/02/12/23fb6a7e8121bb33040424a86f83991c.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com