gpt4 book ai didi

ios - 离开 UIView 时,UILongPressGestureRecognizer 不跟踪触摸

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:17 24 4
gpt4 key购买 nike

我正在使用 UILongPressGestureRecognizer 跟踪 UIView 内的触摸,这就是我的代码

-(void)tapGes:(UILongPressGestureRecognizer *)tapGesture
{
CGPoint location=[tapGesture locationInView:self.view];
UIView *view=tapGesture.view;
switch (tapGesture.state)
{
case UIGestureRecognizerStateBegan:
NSLog(@"enter");
break;
case UIGestureRecognizerStateEnded:
NSLog(@"ended");
break;
case UIGestureRecognizerStateChanged:
if (location.x>view.frame.size.width ||
location.y>view.frame.size.height)
{
NSLog(@"out");
return;
}
NSLog(@"change");
break;
default:
break;
}
}

当处于 UIGestureRecognizerStateChanged 状态的触摸离开 UIView 时,我想做什么我不再跟踪它,这样我就不会进入 UIGestureRecognizerStateEnded 状态如何实现这一目标?

最佳答案

您始终可以向层次结构添加额外的 View 并将手势添加到该 View 。这将使您能够跟踪手势位置并根据 subview 的框架检查它。我的意思是,如果您当前的 View 是 100x100 点,您可以为它制作一个透明的父 View ,即 120x120 点。将手势添加到 120 点 View ,然后将手势的位置与 View 第一个 subview 的框架进行比较。

-(void)tapGes:(UILongPressGestureRecognizer *)tapGesture
{
CGPoint location=[tapGesture locationInView:tapGesture.view];
UIView *view = tapGesture.view;

switch (tapGesture.state)
{
case UIGestureRecognizerStateBegan:
NSLog(@"enter");
break;
case UIGestureRecognizerStateEnded:
NSLog(@"ended");
break;
case UIGestureRecognizerStateChanged:
if (CGRectContainsPoint([view.subviews[0] frame], location)) {
// location is within view
}else{
// location has exited view
}
NSLog(@"change");
break;
default:
break;
}
}

关于ios - 离开 UIView 时,UILongPressGestureRecognizer 不跟踪触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18559382/

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