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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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:
1
2
3
4
5
6
|
mCraftARSDK.setOnContentClickListener(new CraftARTouchEventInterface.OnContentClickListener() {
@Override
public void onClick(CraftARContent craftARContent) {
}
});
|
Touch events on iOS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// 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;
}
}
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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:
1
2
3
4
|
- (void) contentTouchUp {
[super contentTouchUp];
// Your code here
}
|
Comments
0 comments
Article is closed for comments.