简体   繁体   English

绕圆旋转

[英]Rotate around circle

I need to implement the following application in objective c >> http://bit.ly/rHzVkF 我需要在目标c中实现以下应用程序>> http://bit.ly/rHzVkF

How can I rotate a circle by draging like in the app ? 如何通过在应用程序中拖动来旋转圆圈? Do you know some similar examples ? 你知道一些类似的例子吗?

In your touches began method you need to check the x and y and see if it's in the boundaries of the two spinning tabs. 在触摸开始方法中,您需要检查x和y并查看它是否在两个旋转选项卡的边界内。

Now on your touches moved you need to detect the change (delta) of your x and y location and use ATAN2(dx,dy) I believe to convert to radians for revolution. 现在,您感动了,您需要检测x和y位置的变化(增量),并使用ATAN2(dx,dy)相信可以转换为弧度进行旋转。 If I'm wrong you can figure it out on paper, it's simple trigonometry (Don't forget to import <Math.h> ) 如果我错了,可以在纸上弄清楚,这是简单的三角函数(不要忘记导入<Math.h>

With this new variable for revolutions apply it to the object you want to spin using CGAffineTransformRotate(); 使用这个新的旋转变量,可以使用CGAffineTransformRotate();将其应用于您要旋转的对象CGAffineTransformRotate(); (I believe this is part of Core Graphics so make sure you've imported that library as well). (我相信这是Core Graphics的一部分,因此请确保也已导入该库)。

@ihkawiss do you know how to draw a circle with cocoa-touch? @ihkawiss你知道如何用可可粉画一个圆吗? – vikingosegundo – vikingosegundo

@ihkawiss, I guess, you dont want to answer me. @ihkawiss,我想,你不想回答我。 no problem. 没问题。 Your taks needs to be divided in smaller tasks. 您的任务需要划分成较小的任务。

  1. draw small circles on given default position (the handles) 在给定的默认位置上绘制小圆圈(手柄)
  2. draw big circle 画一个大圆圈
  3. get touch coordinates and check, if it is inside a handle, proceed to next point. 获取触摸坐标并检查是否在手柄内,请继续进行下一点。
  4. calculate angle between circle center, touch position and a fixed point (ie.: top center). 计算圆心,触摸位置和固定点之间的角度(即:顶部中心)。
  5. with the angle, fixed point and radius of the circle calculate the new positions of the handles 用角度,圆的固定点和半径计算手柄的新位置
  6. re-draw everything as long as a touch is triggered. 只要触发触摸即可重新绘制所有内容。

you should be able to get enough information for each of this steps in the net and on stack overflow. 您应该能够在网络中以及堆栈溢出时获得足够的信息以用于每个步骤。 If you get stuck (what would be quite normal and to be expected), come back, show what you get so far and explain, where you got stuck. 如果您被卡住了(应该很正常并且可以预料),请回来,显示您到目前为止所得到的,并说明您被卡住的地方。

I will attempt to put my two cents into this as I just had to solve a similar scenario. 我将投入两分钱,因为我不得不解决类似的情况。

you have to implement the touches methods in the view that you want to handle all of the movement 您必须在要处理所有运动的视图中实现touches方法

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event (void)touchesBegan:(NSSet *)touch withEvent:(UIEvent *)event
  • (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event (void)touchesMoved:(NSSet *)touch withEvent:(UIEvent *)event
  • (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event (void)touchesEnded:(NSSet *)touch withEvent:(UIEvent *)event

Albert is right in writing that you need to detect the change of your x and y location. 阿尔伯特写对了,您需要检测x和y位置的变化。 You can get the location from the NSSet touches that is sent into the method on trigger of the method. 您可以从方法触发时发送到方法的NSSet触摸中获取位置。

In the methods you will need to implement some variation of this code in order to work with the touches data: 在这些方法中,您将需要实现此代码的某些变体才能使用touchs数据:

for(UITouch *t in touches){
    if([t tapCount] > 1){
        return;
    }
    CGPoint loc = [t locationInView:self.view];

Once you have the x and y of where a user touched, in this case the CGPoint loc, you can run the atan2 method from the math.h library and get back the radian of the circle. 一旦获得了用户触摸的位置的x和y(在本例中为CGPoint位置),就可以从math.h库中运行atan2方法,并获取圆的弧度。 You will need to #include < math.h > in order to use atan2() 您需要#include <math.h>才能使用atan2()

atan2() needs two arguments in order to work, the difference between two y points, and two x points. atan2()需要两个参数才能起作用,两个y点和两个x点之间的差。 In my case I used the current (touch y point, - center of the circle y), and the current (touch x point, - center of the circle x). 在我的情况下,我使用当前(触摸y点,-圆y的中心)和当前(触摸x点,-圆x的中心)。

    double rads = atan2((loc.y - (self.frame.size.height/2)), (loc.x - (self.frame.size.width/2))); 

from there you can do any number of things with that information. 从那里,您可以利用这些信息来做很多事情。 To calculate the new x and y position around the circle you need to utilize the cosine and sine functions to calculate the proper coordinates. 要计算围绕圆的新x和y位置,您需要利用余弦和正弦函数来计算适当的坐标。

   newXposition = CenterX + cos(rads) * radiusOfCircle
   newYposition = CenterY + sin(rads) * radiusOfCircle

You can get your new xPosition by adding the center X position of your circle to the result of the cos(inputting the radians we just got from before) multiplied by the radius of your circle And the new yposition is obtained by doing the same thing just with sin()instead. 您可以通过将圆的中心X位置加到cos的结果中(输入我们刚刚得到的弧度)乘以圆的半径,来获得新的xPosition。用sin()代替。

So my code looked something like this.... 所以我的代码看起来像这样...

    float radius = 61;
    float newXPos = 140 + (cos(rads) * radius);
    float newYPos = 165 + (sin(rads) * radius);

Good luck, hope this helps you get started. 祝您好运,希望这对您有所帮助。 This stuff can be tricky 这东西可能很棘手

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM