NIK*_*HIL 4 iphone touch uitouch iphone-sdk-3.0
我想跟踪Touch的移动速度像素/秒
任何Budy有这个想法
PanGesture方法除外
Dav*_*ave 11
我假设你想知道连续运动之间的速度,而不是一系列运动的平均速度.如果你有其他要求,理论应该很容易适应.
你需要知道三件事:
然后只需使用毕达哥拉斯来获得你的距离,并按时间划分速度.
当您获得touchesMoved事件时,它将为您提供当前和之前的位置.您需要添加的所有内容都是时间的衡量标准.
为此,您需要类中的NSDate属性来帮助计算时间间隔.您可以在viewDidLoad/viewDidUnload或类似的地方初始化和释放它.在我的例子中,我的名字叫lastTouchTime,它被保留,我初始化如下:
self.lastTouchTime = [NSDate date];
Run Code Online (Sandbox Code Playgroud)
我以可预测的方式发布它.
你的touchesMoved事件应该是这样的:
- (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);
}
Run Code Online (Sandbox Code Playgroud)
抱歉,如果我对任何内存管理做出了失礼,我对ObjectiveC的做事方式仍然不太满意.我敢肯定有人可以在必要时纠正它!
祝你好运,冻结.
| 归档时间: |
|
| 查看次数: |
2502 次 |
| 最近记录: |