Get Started         Best Practices         Developer Guide         FAQ        

Tutorial: Touch events on AR Contents in CraftAR

Last Updated: Mar 18, 2016
This section applies only to the Augmented Reality SDK v4 +. For earlier versions of the Augmented Reality SDK, check out the section below
 

In case you’re not familiar with AR Contents, we suggest you to read the article Overview of Augmented Reality Contents in CraftAR

Touch events in AR contents are triggered when a user touches the screen interacting with the content. We classify these interactions as:

  • Touch in: Movement into a content of an AR Item (swipe into the content)
  • Touch out : A movement out of a content of an AR Item (swipe out of the content)
  • Touch down: A content has been pressed
  • Touch up: A press was released (user stopped touching the screen), when the user was touching this content

Touch events on Android

In order to handle the touch events in Android, you just need to set the OnTouchEventListener to the CraftAR SDK instance as follows:

mCraftARSDK.setOnContentTouchListener(new CraftARTouchEventInterface.OnTouchEventListener() {
    @Override
    public void onTouchIn(CraftARContent craftARContent) {

    }

    @Override
    public void onTouchOut(CraftARContent craftARContent) {

    }

    @Override
    public void onTouchDown(CraftARContent craftARContent) {

    }

    @Override
    public void onTouchUp(CraftARContent craftARContent) {

    }
});

You can also handle the click event individually by setting the OnContentClickListener:

mCraftARSDK.setOnContentClickListener(new CraftARTouchEventInterface.OnContentClickListener() {
    @Override
    public void onClick(CraftARContent craftARContent) {

    }
});

Touch events on iOS

@interface AR_ProgrammaticallyViewController ()

@end

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Get the instance of the SDK and become delegate
    mSDK = [CraftARSDK sharedCraftARSDK];
    mSDK.delegate = self;

    ...

}
Once you have the protocol and the delegate set up, you just need to implement the method to receive the events when the screen is touched over some contents:
// The CraftARContentEventsProtocol protocol allows to receive events for specific contents
// You can navigate to the parent item of a content using content.parentARItem
- (void) didGetTouchEvent:(CraftARContentTouchEvent)event forContent:(CraftARTrackingContent *)content {
    switch (event) {
        case CRAFTAR_CONTENT_TOUCH_IN:
            NSLog(@"Touch in: %@", content.uuid);
            break;
        case CRAFTAR_CONTENT_TOUCH_OUT:
            NSLog(@"Touch out: %@", content.uuid);
            break;
        case CRAFTAR_CONTENT_TOUCH_UP:
            NSLog(@"Touch up: %@", content.uuid);
            break;
        case CRAFTAR_CONTENT_TOUCH_DOWN:
            NSLog(@"Touch down: %@", content.uuid);
            break;
        default:
            break;
    }
}


 
This section applies only to the Augmented Reality SDK v3 or earlier. For the Augmented Reality SDK v4 or newer, check out the section above
 

There are certain applications, such as interactive catalogues, where the user is interested in interacting with augmented reality experiences.

This article describes how to listen to touch events on customized AR Contents. In order to customize contents, you’ll need to follow the steps described in the Tutorial: Customize AR Contents.

In case you’re not familiar with AR Contents, we suggest you to read the article Overview of Augmented Reality Contents in CraftAR

Touch events in AR contents are triggered when a user touches the screen interacting with the content. We classify these interactions as:

  • Touch in: Movement into a content of an AR Item (swipe into the content)
  • Touch out : A movement out of a content of an AR Item (swipe out of the content)
  • Touch down: A content has been pressed
  • Touch up: A press was released (user stopped touching the screen), when the user was touching this content

Touch events on Android

In order to handle the touch events in Android, extend the specific CraftARTrackingContent class and provide a OnTouchEventListener. In the following example, we extend the ImageButton class:

public class MyImageContent extends CraftARTrackingContentImage implements OnTouchEventListener {
    public MyImageContent(String imageUri){
        super(imageUri);
        setContentTouchEventListener(this);
    }
    public MyImageContent(JSONObject object) throws CraftARSDKException {
        super(object);
        setContentTouchEventListener(this);
    }

    @Override
    public void onTouchIn(CraftARTrackingContent content) {
    }

    @Override
    public void onTouchOut(CraftARTrackingContent content) {
    }

    @Override
    public void onTouchDown(CraftARTrackingContent content) {
    }

    @Override
    public void onTouchUp(CraftARTrackingContent content) {
    }
}
Handling click events directly

The CraftAR Augmented Reality SDK for Android also provides an interface to handle click events directly. Click events are triggered when a user performs a touch-down in a content, followed by a touch-up inside the same content.

In order to add a listener for the click events in any type of ARContent, set an OnContentClickListener for that content:

public class MyImageContent extends CraftARTrackingContentImage implements OnContentClickListener {
    public MyImageContent(String imageUri){
        super(imageUri);
        setContentClickListener(this);
    }
    public MyImageContent(JSONObject object) throws CraftARSDKException {
        super(object);
        setContentClickListener(this);
    }

    @Override
    public void onClick(CraftARTrackingContent content) {
        //The user clicked on this content (Touch down + Touch up).
    }
}
Handling click events on AR Buttons

AR Buttons have a predefined action for click events. If the button has an associated URL, the SDK will launch a browser that opens that URL. You can specify this URL using the ‘Action URL’ field in the CraftAR Creator, or by using the ‘hyperlink_url’ field of the contents in the Management API. By setting a content click listener to a button, that button will no longer open the web browser.

Touch events on iOS

In order to handle the touch events on iOS, it is necessary to extend the specific CraftARTrackingContent (or all of them). In the following example, we extend the ImageButton class:

@interface MyImageButton : CraftARTrackingContentImageButton

// Override the touch events

- (void) contentTouchDown;

- (void) contentTouchUp;

- (void) contentTouchIn;

- (void) contentTouchOut;

@end

Don’t forget to call super in your implementation:

- (void) contentTouchUp {
    [super contentTouchUp];
    // Your code here
}

Couldn't find what you were looking for?

support@catchoom.com
https://cdn.desk.com/
false
catchoom
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
false
Invalid characters found
/customer/en/portal/articles/autocomplete