LeapListener Protocol Reference
Conforms to | NSObject |
Declared in | LeapObjectiveC.h |
Overview
The LeapListener protocol defines a set of methods that you can
implement to respond to NSNotification messages dispatched by a LeapController object.
To use the LeapListener protocol, implement a class adopting the protocol
and assign an instance of that class to a LeapController instance:
MYListener *listener = [[MYListener alloc] init];
LeapController *controller = [[LeapController alloc] initWithListener:listener];
The controller subscribes the LeapListener instance to the appropriate NSNotifications
for the Leap events. When a new frame of data is ready, the controller dispatches an
NSNotification on the main application thread, which is handled by your
[LeapListener onFrame:] implementation.
You can handle the other Leap events, onInit
, onConnect
, onDisconnect
,
onExit
, onFocusGained
, and onFocusLost
in the same manner.
You must have a running NSRunLoop to receive NSNotification objects.
This is usually present and running by default in a Cocoa application.
Calling [LeapController addListener:] takes care subscribing the listener object
to the appropriate notifications. The LeapListener object is the notification observer,
while the LeapController object is the notification sender. You can also subscribe to
notifications manually. For example, to subscribe to the OnFrame message, call:
[[NSNotificationCenter defaultCenter] selector:@selector(onFrame:) name:@"OnFrame" object:controller]]
However, at least one listener must be added to the controller with [LeapController addListener:]
or the controller does not bother to dispatch notification messages.
Using the LeapListener protocol is not mandatory. You can also use
a delegate implementing the LeapDelegate protocol or simply poll the
controller object (as described in the LeapController class overview).
Tasks
-
– onInit:
Dispatched once, when the LeapController has finished initializing.
-
– onConnect:
Dispatched when the LeapController object connects to the Leap software, or when
this ListenerListener object is added to a controller that is already connected. -
– onDisconnect:
Dispatched when the LeapController object disconnects from the Leap software.
The controller can disconnect when the Leap device is unplugged, the
user shuts the Leap software down, or the Leap software encounters an
unrecoverable error. -
– onExit:
Dispatched when this LeapListener object is removed from the LeapController
or the controller instance is destroyed. -
– onFrame:
Dispatched when a new LeapFrame containing hand and finger tracking data is available.
Access the new frame data using the [LeapController frame:] function.
Instance Methods
onConnect:
Dispatched when the LeapController object connects to the Leap software, or when
this ListenerListener object is added to a controller that is already connected.
- (void)onConnect:(NSNotification *)notification
Parameters
- notification
The LeapController object dispatching the notification.
Discussion
Dispatched when the LeapController object connects to the Leap software, or when
this ListenerListener object is added to a controller that is already connected.
- (void)onConnect:(NSNotification *)notification
{
NSLog(@"Connected");
LeapController *aController = (LeapController *)[notification object];
[aController enableGesture:LEAP_GESTURE_TYPE_CIRCLE enable:YES];
//...
}
Declared In
LeapObjectiveC.h
onDisconnect:
Dispatched when the LeapController object disconnects from the Leap software.
The controller can disconnect when the Leap device is unplugged, the
user shuts the Leap software down, or the Leap software encounters an
unrecoverable error.
- (void)onDisconnect:(NSNotification *)notification
Parameters
- notification
The LeapController object dispatching the notification.
Discussion
Dispatched when the LeapController object disconnects from the Leap software.
The controller can disconnect when the Leap device is unplugged, the
user shuts the Leap software down, or the Leap software encounters an
unrecoverable error.
- (void)onDisconnect:(NSNotification *)notification
{
NSLog(@"Disconnected");
}
Note: When you launch a Leap-enabled application in a debugger, the
Leap library does not disconnect from the application. This is to allow
you to step through code without losing the connection because of time outs.
Declared In
LeapObjectiveC.h
onExit:
Dispatched when this LeapListener object is removed from the LeapController
or the controller instance is destroyed.
- (void)onExit:(NSNotification *)notification
Parameters
- notification
The LeapController object dispatching the notification.
Discussion
Dispatched when this LeapListener object is removed from the LeapController
or the controller instance is destroyed.
- (void)onExit:(NSNotification *)notification
{
NSLog(@"Exited");
}
Declared In
LeapObjectiveC.h
onFrame:
Dispatched when a new LeapFrame containing hand and finger tracking data is available.
Access the new frame data using the [LeapController frame:] function.
- (void)onFrame:(NSNotification *)notification
Parameters
- notification
The LeapController object dispatching the notification.
Discussion
Dispatched when a new LeapFrame containing hand and finger tracking data is available.
Access the new frame data using the [LeapController frame:] function.
- (void)onFrame:(NSNotification *)notification
{
NSLog(@"New LeapFrame");
LeapController *controller = (LeapController *)[notification object];
LeapFrame *frame = [controller frame:0];
//...
}
Note, the LeapController skips any pending onFrame notifications while your
onFrame handler executes. If your implementation takes too long to return,
one or more frames can be skipped. The controller still inserts the skipped
frames into the frame history. You can access recent frames by setting
the history parameter when calling the [LeapController frame:] function.
You can determine if any pending onFrame events were skipped by comparing
the ID of the most recent frame with the ID of the last received frame.
Declared In
LeapObjectiveC.h
onInit:
Dispatched once, when the LeapController has finished initializing.
- (void)onInit:(NSNotification *)notification
Parameters
- notification
The LeapController object dispatching the notification.
Discussion
Dispatched once, when the LeapController has finished initializing.
Only the first LeapListener added to the controller receives this notification.
- (void)onInit:(NSNotification *)notification
{
NSLog(@“Initialized”);
//…
}`
Declared In
LeapObjectiveC.h