旋转拨盘 - 旋转限制 [英] rotary dial - rotation limitation

查看:135
本文介绍了旋转拨盘 - 旋转限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个老式手机并获得了一个正常工作的起始码,用户将手指移到UIImageView号码上并旋转拨号。然后它将其移回原始位置。见截图。

I am making a vintage phone and got a working starting code where user moves his fingers over a UIImageView numbers and it rotates dial. It then moves it back to original position. See screenshot.

我似乎无法弄清楚的三个问题是:

The three problems that I can't seem to figure out are;


  1. 如何限制用户仅按顺时针方向旋转?目前用户可以向任何方向移动(顺时针和逆时针)

  1. How can I restrict user to rotate only in clockwise direction? Currently user can move it in any direction (clockwise and counter clockwise)

如何检测用户选择的号码?意味着用户触及1或3或5?我需要这个信息,以便当数字到达右边的栏时我可以停止旋转。

How can I detect which number that user selected? Meaning user touched 1 or 3 or 5? I need this info so that I can stop the rotation when that number reaches the bar on the right.

在我当前的代码中,当我停止旋转并让走了一圈,它逆时针向后移动回到它的位置。如果我选择1,2,3,4,它可以正常工作,但对于任何数字5及以上,表盘顺时针移动回原位。如何在touchesEnded上强制逆时针运动?

In my current code when I stop the rotation and let go of the circle, it moves back to it's place by moving back counter clockwise. It works well if I select 1,2,3,4 but for any number 5 and up the dial moves clockwise back to its original position. How can I force counter clockwise motion on touchesEnded?


推荐答案

让我们假设你在谈论这个姿态:

Let’s assume that you’re talking about this gesture:


来源

构建单点触控旋转手势识别器。正确构建手势识别器后,您只需查看旋转,看看如何处理旋转垫。

Build a single-touch rotation gesture recognizer. After building the gesture recognizer correctly, you can just look at the rotation and see what to do with the rotary pad.

构建一个旋转垫时,您会考虑几件事情。单触旋转手势识别器。如果你看 UIRotationGestureRecognizer ,它会使用两个手指支持的两个触摸之间的连接来导出当前角度,然后将角度与之前的角度进行比较,触摸更改事件,以查看增量。

There are several things you’ll consider when building a single-touch rotation gesture recognizer. If you look at UIRotationGestureRecognizer, it uses connection between two touches, backed by two fingers, to derive the current angle, then compares the angle to the previous angle, derived from an earlier touch change event, to see the delta.

衡量当前角度

形成一条线需要两个点,你需要一条线才能知道角度。如果您只使用一次触摸,则需要一个锚点。有很多方法可以将锚点发送到手势识别器,因为您可能要构建自定义类,请使用委托。

It takes two points to form a line and you need a line to know the angle. If you’re working with only one touch, you need an anchor point. There are many ways to send an anchor point to your gesture recognizer, and since you’re likely going to build a custom class, use delegation.

累积轮换计数

如果您只是记录角度并在触摸更改期间发送消息,它有时会起作用。但是,如果您想要实现滞后(例如,此旋转拨盘只能顺时针旋转一次,然后它会收紧),您需要累计顺时针和逆时针方向的旋转计数。

If you simply note the angle and send off messages during touch changes, it’ll sometimes work. However, if you’d like to implement hysteresis (e.g. this rotary dial will only rotate once clockwise, then it tightens up), you’ll need to accumulate rotation counts for both clockwise and counter-clockwise directions.

幸运的是,您可以假设a)触摸事件不会经常掉落,并且b)简单地将当前角度与过去角度进行比较,看看它们是否越过象限边界就足够了。

Fortunately, you can assume that a) the touch events will not get dropped too often, and b) simply comparing the current angle against the past angle, seeing if they cross quadrant boundaries, will suffice.

例如:


  • 如果触摸从左上象限移动进入右上象限,在旋转计数中加一。

  • 如果触摸从右上象限移动到左上象限,则从旋转计数中减去一。

(是的,这确实有用。)

(Yup, this actually works.)

发出正确的,累积的轮换

如果你想发出完全像 UIRotationGestureRecognizer 那样的轮换信息,您将跟踪四件事。

If you want to emit rotation information exactly like how UIRotationGestureRecognizer did, there will be four things you’re tracking.


  1. 起始角度:来自锚点的连接之间的角度指向起始触摸,以及从锚点到固定参考点的连接。

  2. 当前角度:从锚点到锚点的连接之间的角度当前触摸,以及从锚点到固定参考点的连接。

  3. 旋转计数:从连续比较当前值得到的顺时针旋转次数当前角度与其最后一个值的关系(如上一节所述)。如果触摸逆时针移动,则此计数将变为负数。

  1. Starting Angle: The angle between a connection from the anchor point to the starting touch, and a connection from the anchor point to a fixed reference point.
  2. Current Angle: The angle between a connection from the anchor point to the current touch, and a connection from the anchor point to a fixed reference point.
  3. Rotation Count: The number of clockwise revolutions derived from continuously comparing the current value of Current Angle against its last value (as talked about in the last section). If the touch is moving counter-clockwise, then this count will go into negative.

您将提供旋转计数* 2_PI +(当前角度 - 起始角度)作为旋转。

这篇关于旋转拨盘 - 旋转限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆