Touches Began vs. Tap Gesture Recognizer with ARKit

Viewed 730

I'm working on a project with ARKit, and I'm trying to add objects when I tap on the screen. I used the touchesBegan function and the app seemed to be working fine, adding objects where I pressed. But I then wanted to add a longPressGestureRecognizer so that I could remove a certain object from my AR SceneView, so I decided to scrap the touchesBegan in favor of a tapGestureRecognizer, since the touchesBegan was taking precedence over the longPressGesture.

My issue is that the tap gesture recognizer started adding the AR Objects in more erratic places and as far as I can tell, the code was similar to my touchesBegan. I can't seem to find the difference, and I would rather work with the tapGestureRecognizer, or if there is a way to have the gestureRecognizer work alongside touchesBegan would also be useful.

My code is as follows for the touches Began function

-(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];
}

For the tapGestureRecognizer, my code is as follows:

-(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];
}

The only difference with the sets of code is how the touch is initially generated, from what I can tell.

Any help would be highly appreciated.

0 Answers
Related