Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 我想用每秒像素数来跟踪fingure运动的速度,有可能吗?_Iphone_Iphone Sdk 3.0_Touch_Uitouch - Fatal编程技术网

Iphone 我想用每秒像素数来跟踪fingure运动的速度,有可能吗?

Iphone 我想用每秒像素数来跟踪fingure运动的速度,有可能吗?,iphone,iphone-sdk-3.0,touch,uitouch,Iphone,Iphone Sdk 3.0,Touch,Uitouch,我想跟踪以像素/秒为单位移动的触摸速度 布迪对此有什么想法吗 除了泛感觉法,我假设你想知道连续运动之间的速度,而不是一系列运动的平均值。不过,如果您有其他要求,那么该理论应该很容易适应 你需要知道三件事: 触摸现在在哪里 以前的触摸在哪里 从那时起已经过去了多少时间 然后用毕达哥拉斯算出你的距离,除以时间算出速度 当您获得一个TouchsMoved事件时,它将为您提供当前和以前的位置。然后,您只需要添加一个时间度量 为此,您需要类中的NSDate属性来帮助计算时间间隔。您可以在viewDidLo

我想跟踪以像素/秒为单位移动的触摸速度
布迪对此有什么想法吗


除了泛感觉法,我假设你想知道连续运动之间的速度,而不是一系列运动的平均值。不过,如果您有其他要求,那么该理论应该很容易适应

你需要知道三件事:

  • 触摸现在在哪里
  • 以前的触摸在哪里
  • 从那时起已经过去了多少时间
  • 然后用毕达哥拉斯算出你的距离,除以时间算出速度

    当您获得一个TouchsMoved事件时,它将为您提供当前和以前的位置。然后,您只需要添加一个时间度量

    为此,您需要类中的NSDate属性来帮助计算时间间隔。您可以在viewDidLoad/viewDidUnload或类似的地方初始化并释放它。在我的示例中,我的名为lastTouchTime,它被保留,我的初始化如下:

    self.lastTouchTime = [NSDate date];
    
    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {       
    
        NSArray *touchesArray = [touches allObjects];
        UITouch *touch;
        CGPoint ptTouch;
        CGPoint ptPrevious;
    
        NSDate *now = [[NSDate alloc] init];
        NSTimeInterval interval = [now timeIntervalSinceDate:lastTouchTime];
        [lastTouchTime release];
        lastTouchTime = [[NSDate alloc] init];
        [now release];
    
        touch = [touchesArray objectAtIndex:0];
        ptTouch = [touch locationInView:self.view];
        ptPrevious = [touch previousLocationInView:self.view];
    
        CGFloat xMove = ptTouch.x - ptPrevious.x;
        CGFloat yMove = ptTouch.y - ptPrevious.y;
        CGFloat distance = sqrt ((xMove * xMove) + (yMove * yMove));
    
        NSLog (@"TimeInterval:%f", interval);
        NSLog (@"Move:%5.2f, %5.2f", xMove, yMove);
        NSLog (@"Distance:%5.2f", distance);
        NSLog (@"Speed:%5.2f", distance / interval);
    }
    
    我以可预测的方式发布它

    您的TouchsMoved事件应该如下所示:

    self.lastTouchTime = [NSDate date];
    
    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {       
    
        NSArray *touchesArray = [touches allObjects];
        UITouch *touch;
        CGPoint ptTouch;
        CGPoint ptPrevious;
    
        NSDate *now = [[NSDate alloc] init];
        NSTimeInterval interval = [now timeIntervalSinceDate:lastTouchTime];
        [lastTouchTime release];
        lastTouchTime = [[NSDate alloc] init];
        [now release];
    
        touch = [touchesArray objectAtIndex:0];
        ptTouch = [touch locationInView:self.view];
        ptPrevious = [touch previousLocationInView:self.view];
    
        CGFloat xMove = ptTouch.x - ptPrevious.x;
        CGFloat yMove = ptTouch.y - ptPrevious.y;
        CGFloat distance = sqrt ((xMove * xMove) + (yMove * yMove));
    
        NSLog (@"TimeInterval:%f", interval);
        NSLog (@"Move:%5.2f, %5.2f", xMove, yMove);
        NSLog (@"Distance:%5.2f", distance);
        NSLog (@"Speed:%5.2f", distance / interval);
    }
    
    抱歉,如果我在内存管理方面犯了错误,我仍然对Objective的工作方式不太满意。我相信有人会在必要时纠正它


    祝你好运,冻僵了。

    你能澄清一下你指的是速度还是速度吗?i、 e.如果用户只需1秒就可以画出一个圆,并在开始的同一像素上画完,那么您希望答案是a)等于该圆的周长/1秒,还是b)零。注意b)的实现将被证明更简单,但是在直线上可以很好地工作。不,实际上我想要的是==>如果我当时将fingure移动到屏幕上,我需要PanGesture中提供的每秒像素变化率,但PanGesture不受ios小于3.2的支持,请告诉我你是否有任何想法???谢谢你的支持Reply@Dave:我希望它随机移动在整个屏幕中,您的应用程序是否使用NSTimer scheduledTimerWithTimeInterval常规回调?您想要一段时间内的平均速度,还是只想要检测到的每个运动的速度?我只是想知道如何最好地帮助回答你的问题。如果你看一下平移手势库,我需要每个动作的速度,那里会有一个函数-(CGPoint)velocityView:(UIView*)视图;我希望输出像这样,因为这个方法返回cgpoint,它是每秒点的变化