如何测量滑动的速度?

Rad*_*hus 2 cocoa-touch objective-c

我正在开发一种游戏,它将使用滑动力作为可变用户输入.

我从文档中读到,在touchesEnded事件中,我可以获得allTouches数组,它是从touchesBegan收集的用户触摸列表.由此我计划得到最后两次接触以获得滑动的方向.我还将获得touchesBegan和touchesEnded之间的时间间隔,从中我将获得滑动的速度.我将使用方向和速度来计算滑动的力度.

我想知道的是:有更好的方法吗?这已经封装在某个库中的库调用吗?

提前致谢.

小智 7

像这样解决

- (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];
}
Run Code Online (Sandbox Code Playgroud)


Mel*_*emi 5

我已经沿着这些思路完成了一些事情,而只是简单地通过[touch locationInView:self]与将移动的对象的类(UIView 的子类)[touch previousLocationInView:self]进行比较。当用户从 iPhone 上松开手指时,touchesEnded这将为您提供一个包含位置、方向和粗略速度感的矢量。