获取 UIView 在 TouchMoved 上的旋转方向

发布于 2024-08-25 07:43:53 字数 788 浏览 8 评论 0原文

这可能听起来很有趣,但我花了几个小时尝试使用 UIView 和一些三角函数重新创建具有真实旋转的旋钮。我实现了目标,但现在我不知道如何知道旋钮是向左还是向右旋转。数学中最相关的部分在这里:

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

    float dx = pt.x  - iv.center.x;
    float dy = pt.y  - iv.center.y;
    float ang = atan2(dy,dx);

    //do the rotation
    if (deltaAngle == 0.0) {
        deltaAngle = ang;
        initialTransform = iv.transform;
    }else
    {
        float angleDif = deltaAngle - ang;
        CGAffineTransform newTrans = CGAffineTransformRotate(initialTransform, -angleDif);
        iv.transform = newTrans;
        currentValue = [self goodDegrees:radiansToDegrees(angleDif)];
    }
}

理想情况下,我可以利用一个数值来告诉我旋转是正还是负。

this may sound funny, but i spent hours trying to recreate a knob with a realistic rotation using UIView and some trig. I achieved the goal, but now i can not figure out how to know if the knob is rotating left or right. The most pertinent part of the math is here:

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

    float dx = pt.x  - iv.center.x;
    float dy = pt.y  - iv.center.y;
    float ang = atan2(dy,dx);

    //do the rotation
    if (deltaAngle == 0.0) {
        deltaAngle = ang;
        initialTransform = iv.transform;
    }else
    {
        float angleDif = deltaAngle - ang;
        CGAffineTransform newTrans = CGAffineTransformRotate(initialTransform, -angleDif);
        iv.transform = newTrans;
        currentValue = [self goodDegrees:radiansToDegrees(angleDif)];
    }
}

ideally, i could leverage a numeric value to tell me if the rotation is positive or negative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Hello爱情风 2024-09-01 07:43:53

如果旋钮的中心点是用于旋钮的图像的中心,那么您应该能够检测到触摸是否从中心的左侧/右侧开始并向上/向下移动。如果触摸从左侧开始并向下移动,则它会向左旋转。对于从中心另一侧开始的触球,反之亦然。您可以通过将连续的x坐标放入touchesDidMove方法内的数组中并进行简单的比较来检测触摸是向上还是向下移动。

if your knob's center point is the center of the image you are using for your knob then you should be able to detect if the touch starts on the left / right of the center and moves upwards / downwards. If touch starts on the left and is moving downwards it's being rotated to the left. vice versa for touches that start on the other side of center. you can detect whether touches are moving upwards or downwards by placing successive x coordinates in an array whithin the touchesDidMove method and doing a simple comparison.

玻璃人 2024-09-01 07:43:53

谢谢卸妆师。这接近我所需要的,但最终,由于旋钮对象将事件传递给控制器​​类,我需要一种方法来测量旋转值......即:340* 在 CClockWise 处是 94%。无论如何,我最终使用上面的方法解决了这个问题,并将 currentValue 传递给委托。然后,委托将采用该方法并使用它自己的私有浮点数,比较该值是否高于或低于最后收到的值:

//delegate uses a private value to measure from last call back
#pragma mark UIKnobViewDelegate
- (void)dialValue:(float)value
{
if (lastValue == 0.00) {
lastValue = lastValue;
}
if (value

从那里,我们的控制器可以做任何它喜欢的事情

Thanks Remover. This is close to what i needed, but in the end, since the knob object is passing an event to a controller class, i needed a way to measure value from the rotation...ie: 340* is 94% at CClockWise. Anyway, i ended up resolving this using the method above and passing the currentValue to a delegate. The delegate would then take that method and with it's own private float, compare if the value is higher or lower than the last value received:

//delegate uses a private value to measure from last call back
#pragma mark UIKnobViewDelegate
- (void)dialValue:(float)value
{
if (lastValue == 0.00) {
lastValue = lastValue;
}
if (value

from there, our controller can do anything it likes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文