Users rarely say "this hybrid app has poor gesture handling". They say it "feels off". The difference between a web view that feels like a website and one that feels native is mostly small things: a sheet that follows your finger, motion that responds to input, and a tap of haptic feedback at the right moment. Ionic ships tools for the first two and Capacitor covers the third. This tutorial puts them together.
Examples use plain TypeScript so they port to Angular, React and Vue.
Setup
npm install @capacitor/haptics
npx cap sync
createGesture and createAnimation come from @ionic/core and are already available in any Ionic app.
1. A drag gesture that follows the finger
Attach a gesture to an element and move it with the pointer. Two rules matter: do the work in onMove without triggering framework change detection, and set gestureName and threshold so you don't fight scrolling.
import { createGesture, Gesture } from '@ionic/core';
export function makeDismissible(el: HTMLElement, onDismiss: () => void): Gesture {
const gesture = createGesture({
el,
gestureName: 'card-dismiss',
direction: 'x',
threshold: 10,
onMove: ev => {
el.style.transform = `translateX(${ev.deltaX}px)`;
el.style.opacity = String(1 - Math.min(Math.abs(ev.deltaX) / 300, 0.6));
},
onEnd: ev => {
const shouldDismiss = Math.abs(ev.deltaX) > 120 || Math.abs(ev.velocityX) > 0.5;
el.style.transition = 'transform 200ms ease-out, opacity 200ms ease-out';
el.style.transform = shouldDismiss ? `translateX(${Math.sign(ev.deltaX) * 500}px)` : 'translateX(0)';
el.style.opacity = shouldDismiss ? '0' : '1';
if (shouldDismiss) setTimeout(onDismiss, 200);
},
});
gesture.enable();
return gesture;
}
Using velocity as well as distance is what makes a quick flick work even when the finger didn't travel far. Call gesture.destroy() when the component is torn down.
2. Animations with createAnimation
The Animation API uses the Web Animations API under the hood and can be played, reversed and scrubbed. Stick to transform and opacity, which the browser can run on the compositor.
import { createAnimation } from '@ionic/core';
const pop = createAnimation()
.addElement(document.querySelector('.badge')!)
.duration(250)
.easing('cubic-bezier(0.34, 1.56, 0.64, 1)')
.keyframes([
{ offset: 0, transform: 'scale(0.6)', opacity: 0 },
{ offset: 1, transform: 'scale(1)', opacity: 1 },
]);
await pop.play();
Avoid animating height, top or box-shadow on long lists; those trigger layout and drop frames on mid-range Android phones.
3. Haptics at the right moments
Haptics should confirm an action, not decorate everything.
import { Haptics, ImpactStyle, NotificationType } from '@capacitor/haptics';
export const haptic = {
tap: () => Haptics.impact({ style: ImpactStyle.Light }),
commit: () => Haptics.impact({ style: ImpactStyle.Medium }),
success: () => Haptics.notification({ type: NotificationType.Success }),
error: () => Haptics.notification({ type: NotificationType.Error }),
tick: () => Haptics.selectionChanged(),
};
Good uses: a light tap when a gesture crosses its dismiss threshold, selectionChanged while scrubbing a picker, success after a completed purchase or save, error on a failed validation. Bad uses: every button press, scroll events, or anything that fires repeatedly.
Wire the threshold feedback into the gesture so it fires once when crossed, not on every move:
let armed = false;
// inside onMove
const past = Math.abs(ev.deltaX) > 120;
if (past !== armed) { armed = past; if (past) haptic.commit(); }
On the web (and in the PWA build) the plugin may be unavailable, so wrap calls to fail silently:
const safe = (fn: () => Promise<void>) => () => fn().catch(() => {});
4. Respect reduced motion
Some users turn off motion for accessibility or vestibular reasons. Honour it:
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const duration = reduceMotion ? 0 : 250;
Keep the functional result (the card still dismisses) and drop the flourish. Give users a setting to disable haptics too.
5. Keep it at 60fps
- Do per-frame work in
onMoveonly; read layout once before the gesture starts, not during. - Use
will-change: transformon the dragged element, and remove it afterwards. - Run gesture callbacks outside Angular's zone (
NgZone.runOutsideAngular) in zone-based apps, and re-enter only foronDismiss. - Test on a real mid-range Android device with remote debugging, not just a flagship iPhone.
Checklist
- Gestures follow the finger and use velocity as well as distance.
- Animations touch only
transformandopacity. - Haptics fire once per meaningful event and fail silently on web.
- Reduced-motion is respected; haptics can be turned off.
- Verified on low-end Android hardware.
If your Ionic app works but still doesn't feel right, our consultants run short UX-performance audits on Ionic and Capacitor apps. Get in touch via the contact page.