gpt4 book ai didi

ios - 关于 UIGestures 的困惑 - 消息接收者

转载 作者:行者123 更新时间:2023-11-29 02:12:13 27 4
gpt4 key购买 nike

我不明白我做错了什么

为什么 velocityInView:self.view & locationInView 发送到 UILongPressGestureRecognizer 而不是 UIPanGestureRecognizer

    UILongPressGestureRecognizer *cursorMode =  [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(pan:)];
cursorMode.minimumPressDuration = 0.35;
[self.Row2View addGestureRecognizer:cursorMode];


- (void)pan:(UIPanGestureRecognizer *)gesture {

float position = round([gesture locationInView:self.view].x);
NSLog(@"%f", position);

CGPoint velocity = [gesture velocityInView:self.view];

if(velocity.x* _lastGestureVelocity.x > 0)
{
NSLog(@"gesture went in the same direction");
}
else
{
NSLog(@"gesture went in the opposite direction");
}

_lastGestureVelocity = velocity;
}

错误: *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UILongPressGestureRecognizer velocityInView:]:无法识别的选择器发送到实例 0x6080001b0300”

最佳答案

通过创建 cursorMode 作为平移手势识别器(我想这就是你的意思)或重命名 pan: 方法(到 longPress:)来修复,以及长按 gr 进行参数化。

就目前而言,代码分配了一个长按gr:

UILongPressGestureRecognizer *cursorMode = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

但是将参数转换为 pan: 作为 UIPanGestureRecognizer,向它发送 velocityInView: 消息。这将在运行时崩溃,因为正如您所指出的,它被发送到长按 gr。

编辑 - 根据评论,功能目标似乎是在识别长按后跟踪运动。这可以在不使用平移手势的情况下通过自己保留位置状态来完成:

@property(assign,nonatomic) CGPoint lastLocation;

UILongPressGestureRecognizer *cursorMode = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
cursorMode.minimumPressDuration = 0.35;
[self.Row2View addGestureRecognizer:cursorMode];

- (void)longPress:(UILongPressGestureRecognizer *)gr {
CGPoint location = [gr locationInView:gr.view];
// you can compute the change in location like this
CGPoint delta = CGPointMake(location.x-self.lastLocation.x, location.y-self.lastLocation.y);

// do whatever you want with location or delta, then at the end...
self.lastLocation = location;
}

请注意,坐标系的选择是我们传递给 locationInView: 的 View 的结果。通过按照建议的代码传递 gr.view,我们将跟踪获取手势的 View 内的平移。坐标的另一个常见选择是跟踪 View 的 super View 中的平移。

关于ios - 关于 UIGestures 的困惑 - 消息接收者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29170565/

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