gpt4 book ai didi

iphone - 车速表中的箭头旋转

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:08:21 30 4
gpt4 key购买 nike

我实现了一个速度计。我有一个箭头图像。我想用手指触摸非常平滑地将它旋转 0° 到 180°,而且我应该给出边界。怎么可能?

这是我的代码(在 UIImageView 类别中):

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];

CGPoint currentLocation = [touch locationInView:self.superview];
CGPoint pastLocation = [touch previousLocationInView:self.superview];

CGPoint d1 = CGPointMake(currentLocation.x-self.superview.center.x, currentLocation.y-self.superview.center.y);
CGPoint d2 = CGPointMake(pastLocation.x-self.superview.center.x, pastLocation.y-self.superview.center.y);

CGFloat angle1 = atan2(d1.y, d1.x);
CGFloat angle2 = atan2(d2.y, d2.x);

self.transform = CGAffineTransformRotate(self.transform, angle1-angle2);
}

它正在旋转 0 到 360 度,我需要限制在 20 到 160 度之间的角度。

最佳答案

根据触摸(使用触发)找出要应用的正确仿射变换并应用它。

 view.transform = CGAffineTransformMakeRotation(angleInRadians);

可能您需要先平移原点,然后再平移原点,使其围绕您想要的点旋转。或者,您可以制作 View ,使枢轴点位于 View 的中心。

要计算角度,请找到触摸点与旋转点 (dx, dy) 的距离。

tan angle = dy / dx

使用

 atan2(dy, dx)

因为它通过知道它在哪个象限来处理符号(不要为 dx 传递 0)。

https://developer.apple.com/library/IOs/#documentation/System/Conceptual/ManPages_iPhoneOS/man3/atan2.3.html

分别处理 dx 为 0 的情况(即 PI/2 弧度)。可能还有其他特殊情况需要处理以对其进行约束——只需选择最小和最大弧度并在用于转换之前检查它们。

根据问题更新进行编辑:您已经完成了困难的部分——只需观察角度并限制它

使用 MIN 和 MAX 来限制角度 1 和角度 2(记住它是弧度,所以 20/180*pi160/180*pi)。在传递给 CGAffineTransformRotate 之前就这样做

关于iphone - 车速表中的箭头旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9211551/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com