gpt4 book ai didi

ios - 检测用户触摸移动方向?

转载 作者:行者123 更新时间:2023-11-29 11:59:14 25 4
gpt4 key购买 nike

我使用 UIBezierPath 绘制了字母 A。它具有三个方向来演示用户将如何绘制此字母表。有什么方法可以检查用户触摸移动是否在同一方向?

enter image description here

最佳答案

我推荐一个 UIPanGestureRecognizer。它将为您提供当前位置、从起点开始的平移和当前速度。如果您跟踪速度,您就会知道它是否与之前的速度匹配。

可以使用-[UIBezierPath containsPoint:]判断手势是否还在A内。

像这样:

- (void)panRecognizerFired:(UIPanGestureRecognizer *)panRecognizer {
switch (panRecognizer.state) {
case UIGestureRecognizerStatePossible:
{
// ignore it until it changes to UIGestureRecognizerStateBegan
return;
}
case UIGestureRecognizerStateBegan:
{
// handle gesture start here

// change this to a break if you want a beginning touch to be processed
// the same as a movement
return;
}
case UIGestureRecognizerStateEnded:
{
// handle gesture end here - clean up and undo whatever happened
// in UIGestureRecognizerStateBegan, show a new controller or something, etc

// change this to a break if you want an ending touch to be processed
// the same as a movement
return;
}
case UIGestureRecognizerStateFailed:
case UIGestureRecognizerStateCancelled:
{
// abort! undo everything here
return;
}
default:
// it moved! break to process that movement
break;
}

CGPoint location = [panRecognizer locationInView:self.theViewThatHasTheLetterAInIt];
CGPoint translation = [panRecognizer translationInView:self.theViewThatHasTheLetterAInIt];
CGPoint velocity = [panRecognizer velocityInView:self.theViewThatHasTheLetterAInIt];

BOOL isLocationInsidePath = [self.letterABezierPath containsPoint:location];

// make decisions based on isLocationInsidePath and velocity here...
}

为了确保这个手势只允许在正确的数字附近开始,使 self 符合 UIGestureRecognizerDelegate 协议(protocol)并设置 self.panGestureRecognizer.delegate = self。然后你可以用这个限制起始位置:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == self.panGestureRecognizer) {
CGPoint location = [gestureRecognizer locationInView:self.theViewThatHasTheLetterAInIt];

// implement this property to return the center of the 1, 2, or 3 circle
CGPoint targetCenter = self.currentTargetCenter;

CGFloat distanceX = location.x - targetCenter.x;
CGFloat distanceY = location.y - targetCenter.y;
CGFloat distanceDiagonalSquared = (distanceX * distanceX) + (distanceY * distanceY);

const CGFloat maxDistance = 20.f;

return (distanceDiagonalSquared <= maxDistance * maxDistance);
}

return YES;
}

关于ios - 检测用户触摸移动方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37742056/

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