Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I measure the velocity of a swipe?

I'm developing a game that will use the force of the swipe as a variable user input.

I read from the documentation that on the touchesEnded event, I can get the allTouches array which is a list of the user touches collected from touchesBegan. From this I plan to get the last two touches to get the direction of the swipe. I will also get the time interval between touchesBegan and touchesEnded, from which I will get the speed of the swipe. I will use the direction and the speed to calculate the force of the swipe.

What I'd like to know is: is there a better way to do this? Is this already encapsulated in a library call somewhere?

Thanks in advance.

like image 287
Radamanthus Avatar asked Nov 22 '25 04:11

Radamanthus


1 Answers

Solve like this

- (void)rotateAccordingToAngle:(float)angle
{
    [spinWheel setTransform:CGAffineTransformRotate(spinWheel.transform, angle)];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    [spinWheel.layer removeAllAnimations];
    previousTimestamp = event.timestamp;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (touch.view==spinWheel)
    {
        UITouch *touch = [touches anyObject];
        CGPoint center = CGPointMake(CGRectGetMidX([spinWheel bounds]), CGRectGetMidY([spinWheel bounds]));
        CGPoint currentTouchPoint = [touch locationInView:spinWheel];
        CGPoint previousTouchPoint = [touch previousLocationInView:spinWheel];
        CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

        [self rotateAccordingToAngle:angleInRadians];

        CGFloat angleInDegree = RADIANS_TO_DEGREES(angleInRadians);
        revolutions+= (angleInDegree/360.0f);
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (touch.view==spinWheel) 
    {
        NSTimeInterval timeSincePrevious = event.timestamp - previousTimestamp;
        CGFloat revolutionsPerSecond = revolutions/timeSincePrevious;
        NSLog(@"%.3f",revolutionsPerSecond);
        [self startAnimationWithRevolutions:revolutionsPerSecond forTime:5.0f];
    }
    revolutions = 0;
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    spinWheel.userInteractionEnabled = TRUE;
    if (timerUpdate) {
        [timerUpdate invalidate];
        timerUpdate = nil;
    }
}
-(void)updateTransform{
    spinWheel.transform = [[spinWheel.layer presentationLayer] affineTransform];
}
-(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time
{
    spinWheel.userInteractionEnabled = FALSE;
    float totalRevolutions = revPerSecond * time;

    timerUpdate = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateTransform) userInfo:nil repeats:YES];

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:time] forKey:kCATransactionAnimationDuration];

    CABasicAnimation* spinAnimation = [CABasicAnimation
                                       animationWithKeyPath:@"transform.rotation"];
    CGAffineTransform transform = spinWheel.transform;
    float fromAngle = atan2(transform.b, transform.a);
    float toAngle = fromAngle + (totalRevolutions*4*M_PI);
    spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle];
    spinAnimation.toValue = [NSNumber numberWithFloat:toAngle];
    spinAnimation.repeatCount = 0;
    spinAnimation.removedOnCompletion = NO;
    spinAnimation.delegate = self;
    spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:
                                    kCAMediaTimingFunctionEaseOut];
    [spinWheel.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
    [CATransaction commit];
}
like image 100
Mehul darji Avatar answered Nov 23 '25 20:11

Mehul darji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!