pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • Examples
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

Pebble C API

  • Pebble C API
  • Moddable API (Alloy)
  • PebbleKit JS
  • PebbleKit iOS
  • PebbleKit Android
  • Foundation
    • Alloy
    • App
    • App Communication
    • App Glance
    • AppMessage
    • AppSync
    • AppWorker
    • DataLogging
    • DataStructures
      • UUID
    • Dictation
    • Dictionary
    • Event Service
      • AccelerometerService
      • AlarmService
      • AppFocusService
      • BacklightService
      • BatteryStateService
      • CompassService
      • ConnectionService
      • HealthService
      • TickTimerService
      • TouchService
    • Exit Reason
    • Internationalization
    • Launch Reason
    • Logging
    • Math
    • Memory Management
    • Platform
    • Resources
      • File Formats
    • Storage
    • Timer
    • Wakeup
    • Wall Time
    • WatchInfo
  • Graphics
    • Draw Commands
    • Drawing Paths
    • Drawing Primitives
    • Drawing Text
    • Fonts
    • Graphics Context
    • Graphics Types
      • Color Definitions
  • User Interface
    • Animation
      • PropertyAnimation
    • Clicks
    • Gesture Recognizers
    • Layers
      • ActionBarLayer
      • BitmapLayer
      • MenuLayer
      • RotBitmapLayer
      • ScrollLayer
      • SimpleMenuLayer
      • StatusBarLayer
      • TextLayer
    • Light
    • Preferences
    • Speaker
    • UnobstructedArea
    • Vibes
    • Window
      • ActionMenu
      • NumberWindow
    • Window Stack
  • Standard C
    • Format
    • Locale
    • Math
    • Memory
    • String
    • Time

The APIs on this page will only work with SDK 4.9+.

Gesture Recognizers

Low-level touch-gesture recognizers for building fully custom touch interactions on touch-capable hardware.

A recognizer watches the raw touch stream for one gesture — a tap, a pan (single-axis drag) or a swipe — and calls a RecognizerEventCb as the gesture progresses. Create one with a per-gesture constructor (tap_recognizer_create, pan_recognizer_create, swipe_recognizer_create), attach it to a Window, and read the current gesture data from the event callback. Recognizers are the building block for a custom scroll or drag.

For an ordinary scrollable list you do not need recognizers at all: on touch hardware MenuLayer already scrolls by touch. Reach for recognizers to build an interaction the built-in list does not give you — a custom menu, a drag, or acting on a raw tap or swipe. Note that a bare ScrollLayer does not scroll by touch on its own; you drive it with a pan recognizer, as below.

Usage example: scrolling a custom menu

A ScrollLayer holds your own content but does not scroll by touch by itself. Attach a vertical pan recognizer to the window and feed its delta into scroll_layer_set_content_offset() to scroll the content by finger:

static ScrollLayer *s_scroll;
static int16_t s_base;  // content offset committed on Complete

static void pan_handler(const Recognizer *recognizer, RecognizerEvent event) {
  switch (event) {
    case RecognizerEvent_Updated: {
      // delta_since_start is (0, 0) at Start, so the content does not jump.
      GPoint d = pan_recognizer_get_delta_since_start(recognizer);
      scroll_layer_set_content_offset(s_scroll, GPoint(0, s_base + d.y), false);
      break;
    }
    case RecognizerEvent_Completed:
      s_base = scroll_layer_get_content_offset(s_scroll).y;  // commit
      break;
    case RecognizerEvent_Cancelled:
      scroll_layer_set_content_offset(s_scroll, GPoint(0, s_base), true);  // roll back
      break;
    default:
      break;
  }
}

static void window_load(Window *window) {
  Layer *root = window_get_root_layer(window);
  s_scroll = scroll_layer_create(layer_get_bounds(root));
  scroll_layer_set_content_size(s_scroll, GSize(layer_get_bounds(root).size.w,
                                                total_content_height));
  // Add your custom row layers as children of s_scroll here.
  layer_add_child(root, scroll_layer_get_layer(s_scroll));

  // The window owns the recognizer and destroys it when the window unloads.
  Recognizer *pan = pan_recognizer_create(pan_handler, NULL, PanAxis_Vertical);
  window_attach_recognizer(window, pan);
}
While the global system recognizer set is live, attaching alone is not enough: a window opts out of it with window_set_touch_bridge_disabled() (added by the touch-bridge change) so its own recognizers receive the touch stream.

Function Documentation

Recognizer * tap_recognizer_create(RecognizerEventCb event_cb, void * user_data)

Create a tap recognizer. The default recognizer recognizes a single tap from a single finger.

Parameters

event_cb

event callback

user_data

user data associated with recognizer

Returns

recognizer reference

Recognizer * pan_recognizer_create(RecognizerEventCb event_cb, void * user_data, PanAxis axis)

Create a pan recognizer locked to a single axis. The recognizer stays in the Possible state until the finger moves unambiguously along axis and crosses the start threshold, at which point it Starts. It Fails if the finger instead moves unambiguously along the foreign axis.

Parameters

event_cb

event callback

user_data

user data associated with recognizer

axis

axis to which the pan is locked

Returns

recognizer reference

Recognizer * swipe_recognizer_create(RecognizerEventCb event_cb, void * user_data, uint8_t direction_mask)

Create a swipe recognizer that accepts the directions set in direction_mask. The recognizer stays Possible while tracking the path and Completes on liftoff if the path is a fast, straight, long-enough flick whose direction is in the mask; otherwise it Fails.

Parameters

event_cb

event callback

user_data

user data associated with recognizer

direction_mask

bitwise-OR of the SwipeDirection values to accept

Returns

recognizer reference

GPoint tap_recognizer_get_tap_point(const Recognizer * recognizer)

Get the coordinate of the recognized tap. The coordinate is taken from the last position update, not the liftoff. Valid once the recognizer has completed.

Parameters

recognizer

recognizer from which to get the tap coordinate

Returns

tap coordinate

GPoint pan_recognizer_get_total_delta(const Recognizer * recognizer)

Get the total movement of the pan since the touchdown point. Component-wise.

Parameters

recognizer

recognizer from which to get the delta

Returns

total delta from touchdown

GPoint pan_recognizer_get_delta_since_start(const Recognizer * recognizer)

Get the movement of the pan since it Started (i.e. since the start threshold was crossed). This is exactly (0, 0) at the instant the recognizer transitions to Started, so live scroll that consumes this value does not jump at gesture start. Component-wise.

Parameters

recognizer

recognizer from which to get the delta

Returns

delta since the pan started

GPoint pan_recognizer_get_delta_since_prev(const Recognizer * recognizer)

Get the movement of the pan since the previous position update. Component-wise.

Parameters

recognizer

recognizer from which to get the delta

Returns

delta since the previous event

GPoint pan_recognizer_get_velocity(const Recognizer * recognizer)

Get the current velocity of the pan, in pixels per second, component-wise. Computed over the most-recent events within a short time window (see PAN_VELOCITY_WINDOW_MS). Zero when the elapsed time across the sampled events is zero.

Parameters

recognizer

recognizer from which to get the velocity

Returns

velocity in px/s

SwipeDirection swipe_recognizer_get_direction(const Recognizer * recognizer)

Get the recognized swipe direction. Valid once the recognizer has Completed; otherwise SwipeDirection_None.

Parameters

recognizer

recognizer from which to get the direction

Returns

recognized swipe direction

GPoint swipe_recognizer_get_velocity(const Recognizer * recognizer)

Get the velocity of the swipe, in pixels per second, component-wise. Computed over the most-recent events within a short time window. Zero when the elapsed time is zero.

Parameters

recognizer

recognizer from which to get the velocity

Returns

velocity in px/s

void recognizer_destroy(Recognizer * recognizer)

Destroy an un-owned recognizer. If a recognizer is not owned, this will destroy the recognizer freeing it's data and calling the destructor (see recognizer_set_on_destroy), if set. If it is owned, this will do nothing.

Parameters

recognizer

Gesture Recognizers to destroy

void recognizer_set_simultaneous_with(Recognizer * recognizer, RecognizerSimultaneousWithCb simultaneous_with_cb)

Specify a callback to determine whether a recognizer should be evaluated simultaneously with another recognizer.

Parameters

recognizer

recognizer to modify

simultaneous_with_cb

callback that determines whether this recognizer will be evaluated simultaneous with another recognizer

void recognizer_set_fail_after(Recognizer * recognizer, Recognizer * fail_after)

Tell a recognizer to only evaluate after the another recognizer fails.

Parameters

recognizer

recognizer to modify

fail_after

recognizer after which the modified gesture will be evaluated

Enum Documentation

enum RecognizerEvent

Enumerators

RecognizerEvent_Started
RecognizerEvent_Updated
RecognizerEvent_Completed
RecognizerEvent_Cancelled
enum PanAxis

Axis to which a pan recognizer is locked. A pan is only recognized when the finger moves unambiguously along this axis.

Enumerators

PanAxis_Horizontal
PanAxis_Vertical
enum SwipeDirection

Swipe direction, also used as a bitmask when configuring which directions a swipe recognizer accepts. Screen coordinates grow downward, so a positive y delta is a downward swipe.

Enumerators

SwipeDirection_None
SwipeDirection_Up
SwipeDirection_Down
SwipeDirection_Left
SwipeDirection_Right

Typedef Documentation

typedef struct Recognizer Recognizer
typedef void(* RecognizerEventCb)(const Recognizer *recognizer, RecognizerEvent event_type)

User event callback. When a recognizer changes state to any state other than the failed state the user callback of this type will be called.

Parameters

recognizer

recognizer affected by the event

event_type

event that occurred

typedef bool(* RecognizerSimultaneousWithCb)(const Recognizer *recognizer, const Recognizer *simultaneous_with)

This function is called to determine whether recognizer should be evaluated simultaneously with simultaneous_with.

Parameters

recognizer

recognizer to be tested

simultaneous_with

recognizer with which recognizer would be evaluated simultaneously should this test pass

Returns

true if recognizers should be evaluated simultaneously

Need some help?

Functions

  • tap_recognizer_create
  • pan_recognizer_create
  • swipe_recognizer_create
  • tap_recognizer_get_tap_point
  • pan_recognizer_get_total_delta
  • pan_recognizer_get_delta_since_start
  • pan_recognizer_get_delta_since_prev
  • pan_recognizer_get_velocity
  • swipe_recognizer_get_direction
  • swipe_recognizer_get_velocity
  • recognizer_destroy
  • recognizer_set_simultaneous_with
  • recognizer_set_fail_after

Enums

  • RecognizerEvent
  • PanAxis
  • SwipeDirection

Typedefs

  • Recognizer
  • RecognizerEventCb
  • RecognizerSimultaneousWithCb

Getting Help

Do you have questions about the Pebble SDK?

Do you need some help understanding something on this page?

You can either take advantage of our awesome developer community and check out the SDK Help forums, or you can join us on the Discord!