API Reference 0.7.1rikulo_gestureZoomGesture

ZoomGesture class

A gesture defined by touch events of two fingers, as an angle-preserving linear transforamtion, which can be naturally decomposed into the following aspects: + Scalar: the ratio of change of the distance between two fingers. + Rotation: the angular change of relative direction of the two fingers. + Transition: the displacement of the midpoint of two fingers.

class ZoomGesture extends Gesture {
 final ZoomGestureStart _start;
 final ZoomGestureMove _move;
 final ZoomGestureEnd _end;
 
 StreamSubscription<Event> _subStart, _subMove, _subEnd;
 ZoomGestureState _state;
 bool _disabled = false;
 
 /** Create a zoom gesture.
  * 
  * + [owner] is the owner who shall listen to touch event.
  * + [start] is called when the second finger involves in the touch events.
  * + [move] is called continuously during the movement of the two fingers.
  * + [end] is called when a finger leaves, or when a third finger joins in 
  * the touch events.
  */
 ZoomGesture(this.owner, {ZoomGestureStart start, ZoomGestureMove move, 
 ZoomGestureEnd end}) : _start = start, _move = move, _end = end {
   
   // listen
   _subStart = owner.onTouchStart.listen((TouchEvent event) {
     final touches = event.touches,
           fingers = touches.length;
     if (fingers > 2) {
       _onEnd(event.timeStamp);
     
     } else if (fingers == 2) {
       final pt0 = touches[0].page,
             pt1 = touches[1].page;
       // t0 and t1 have to be different
       if (pt0.x != pt1.x || pt0.y != pt1.y) {
         _onStart(event.target, pt0, pt1, event.timeStamp);
         event.preventDefault();
       }
     }
   });
   _subMove = owner.onTouchMove.listen((TouchEvent event) {
     final touches = event.touches;
     if (touches.length == 2) {
       _onMove(touches[0].page, touches[1].page, event.timeStamp);
     }
   });
   _subEnd = owner.onTouchEnd.listen((TouchEvent event) => _onEnd(event.timeStamp));
 }
 
 /** The element to which the gesture applies.
  */
 final Element owner;
 
 void stop() {
   _state = null;
 }
 
 void destroy() {
   stop();
   
   // unlisten
   if (_subStart != null) {
     _subStart.cancel();
     _subStart = null;
   }
   if (_subMove != null) {
     _subMove.cancel();
     _subMove = null;
   }
   if (_subEnd != null) {
     _subEnd.cancel();
     _subEnd = null;
   }
 }
 
 void disable() {
   stop();
   _disabled = true;
 }
 
 void enable() {
   _disabled = false;
 }
 
 void _onStart(EventTarget target, Point pos0, Point pos1, int time) {
   if (_disabled)
     return;
   
   stop();
   _state = new ZoomGestureState._(this, target, pos0, pos1, time);
   
   if (_start != null && identical(_start(_state), false))
     stop();
 }
 
 void _onMove(Point pos0, Point pos1, int time) {
   if (_state != null) {
     _state.snapshot(pos0, pos1, time);
     if (_move != null && identical(_move(_state), false))
       stop();
   }
 }
 
 void _onEnd(int time) {
   if (_state != null && _end != null)
     _end(_state);
   stop();
 }
 
}

Extends

Gesture > ZoomGesture

Constructors

new ZoomGesture(Element owner, {ZoomGestureStart start, ZoomGestureMove move, ZoomGestureEnd end}) #

Create a zoom gesture.

  • owner is the owner who shall listen to touch event.
  • start is called when the second finger involves in the touch events.
  • move is called continuously during the movement of the two fingers.
  • end is called when a finger leaves, or when a third finger joins in the touch events.

ZoomGesture(this.owner, {ZoomGestureStart start, ZoomGestureMove move, 
ZoomGestureEnd end}) : _start = start, _move = move, _end = end {
 
 // listen
 _subStart = owner.onTouchStart.listen((TouchEvent event) {
   final touches = event.touches,
         fingers = touches.length;
   if (fingers > 2) {
     _onEnd(event.timeStamp);
   
   } else if (fingers == 2) {
     final pt0 = touches[0].page,
           pt1 = touches[1].page;
     // t0 and t1 have to be different
     if (pt0.x != pt1.x || pt0.y != pt1.y) {
       _onStart(event.target, pt0, pt1, event.timeStamp);
       event.preventDefault();
     }
   }
 });
 _subMove = owner.onTouchMove.listen((TouchEvent event) {
   final touches = event.touches;
   if (touches.length == 2) {
     _onMove(touches[0].page, touches[1].page, event.timeStamp);
   }
 });
 _subEnd = owner.onTouchEnd.listen((TouchEvent event) => _onEnd(event.timeStamp));
}

Properties

final Element owner #

The element to which the gesture applies.

final Element owner

Methods

void destroy() #

Destroy the gesture. It shall be called to clean up, if it is no longer in use.

docs inherited from Gesture
void destroy() {
 stop();
 
 // unlisten
 if (_subStart != null) {
   _subStart.cancel();
   _subStart = null;
 }
 if (_subMove != null) {
   _subMove.cancel();
   _subMove = null;
 }
 if (_subEnd != null) {
   _subEnd.cancel();
   _subEnd = null;
 }
}

void disable() #

Disable the gesture. The current action will be immediately stopped, if any.

docs inherited from Gesture
void disable() {
 stop();
 _disabled = true;
}

void enable() #

Enable the gesture.

docs inherited from Gesture
void enable() {
 _disabled = false;
}

void stop() #

Stop the current action, if any.

docs inherited from Gesture
void stop() {
 _state = null;
}