开发者

get rotation direction of UIView on touchesMoved

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)];
    }
}

ide开发者_开发问答ally, i could leverage a numeric value to tell me if the rotation is positive or negative.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜