gpt4 book ai didi

iphone - UITouch touchesMoved手指方向和速度

转载 作者:技术小花猫 更新时间:2023-10-29 10:44:08 33 4
gpt4 key购买 nike

如何在 touchmoved 函数中获取手指移动的速度和方向?

我想获取手指速度和手指方向并将其应用于 UIView 类方向移动和动画速度。

我阅读了这个链接,但我无法理解答案,此外它也没有解释我如何检测方向:

UITouch movement speed detection

到目前为止,我试过这段代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *anyTouch = [touches anyObject];
CGPoint touchLocation = [anyTouch locationInView:self.view];
//NSLog(@"touch %f", touchLocation.x);
player.center = touchLocation;
[player setNeedsDisplay];
self.previousTimestamp = event.timestamp;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
CGPoint prevLocation = [touch previousLocationInView:self.view];
CGFloat distanceFromPrevious = [self distanceBetweenPoints:location :prevLocation];
NSTimeInterval timeSincePrevious = event.timestamp - previousTimestamp;

NSLog(@"diff time %f", timeSincePrevious);
}

最佳答案

方向将根据 touchesMoved 中的“location”和“prevLocation”的值确定。具体来说,location 将包含新的触摸点。例如:

if (location.x - prevLocation.x > 0) {
//finger touch went right
} else {
//finger touch went left
}
if (location.y - prevLocation.y > 0) {
//finger touch went upwards
} else {
//finger touch went downwards
}

现在 touchesMoved 将针对给定的手指移动多次调用。将手指第一次触摸屏幕时的初始值与移动最终完成时的 CGPoint 值进行比较是代码的关键。

关于iphone - UITouch touchesMoved手指方向和速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10271883/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com