我正在使用ARKit开展项目,我在点击屏幕时尝试添加对象。我使用了touchesBegan函数,应用程序似乎工作正常,添加了我按下的对象。但我想添加一个longPressGestureRecognizer,以便我可以从我的AR SceneView中删除某个对象,所以我决定废弃touchesBegan,转而使用tapGestureRecognizer,因为touchesBegan优先于longPressGesture。
我的问题是,轻敲手势识别器开始在更不稳定的地方添加AR对象,据我所知,代码类似于我的touchesBegan。我似乎找不到差异,我宁愿使用tapGestureRecognizer,或者如果有一种方法让gestureRecognizer与touchesBegan一起工作也会很有用。
触摸功能
的代码如下-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSArray <UITouch *> *touchArray = [touches allObjects];
UITouch *touch = [touchArray firstObject];
NSArray <ARHitTestResult *> *resultArray = [_sceneView hitTest:[touch locationInView:_sceneView] types:ARHitTestResultTypeFeaturePoint];
ARHitTestResult *result = [resultArray lastObject];
SCNMatrix4 hitTransform = SCNMatrix4FromMat4(result.worldTransform);
SCNVector3 hitVector = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43);
[self insertCandleWithPosition:hitVector];
}
对于tapGestureRecognizer,我的代码如下:
-(void)addObject:(UITapGestureRecognizer *)recognizer {
CGPoint tapPoint = [recognizer locationInView:self.view];
NSArray <ARHitTestResult *> *result = [_sceneView hitTest:tapPoint types:ARHitTestResultTypeFeaturePoint];
ARHitTestResult *hitResult = [result lastObject];
SCNMatrix4 hitTransform = SCNMatrix4FromMat4(hitResult.worldTransform);
SCNVector3 hitVector = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m42);
[self insertCandleWithPosition:hitVector];
}
与代码集的唯一区别在于触摸是如何最初生成的,从我所知道的。
任何帮助都将受到高度赞赏。