1- import { DomController , NavController , NavParams , Transition } from 'ionic-angular' ;
2- import { Ion } from 'ionic-angular/components/ion' ;
3- import { PanGesture } from 'ionic-angular/gestures/drag-gesture' ;
4- import { GestureController } from 'ionic-angular/gestures/gesture-controller' ;
5- import { Config } from 'ionic-angular/config/config' ;
6- import { Platform } from 'ionic-angular/platform/platform' ;
7- import { ElementRef , Renderer , Component , OnInit , OnDestroy , NgZone } from '@angular/core' ;
1+ import {
2+ DomController ,
3+ NavController ,
4+ NavParams ,
5+ Transition ,
6+ Ion ,
7+ PanGesture ,
8+ Gesture ,
9+ GestureController ,
10+ Config ,
11+ Platform
12+ } from 'ionic-angular' ;
13+ import { ElementRef , Renderer , Component , OnInit , OnDestroy , NgZone , ViewChild } from '@angular/core' ;
814import { DomSanitizer , SafeUrl } from '@angular/platform-browser' ;
915
1016import { ImageViewerGesture } from './image-viewer-gesture' ;
1117import { ImageViewerEnter , ImageViewerLeave } from './image-viewer-transitions' ;
1218
1319const DOUBLE_TAP_INTERVAL = 300 ;
20+ const MAX_SCALE = 2 ;
1421
1522@Component ( {
1623 selector : 'image-viewer' ,
@@ -23,7 +30,7 @@ const DOUBLE_TAP_INTERVAL = 300;
2330 <ion-backdrop></ion-backdrop>
2431
2532 <div class="image-wrapper">
26- <div class="image">
33+ <div class="image" #imageContainer >
2734 <img [src]="imageUrl" (click)="onImageClick()" tappable />
2835 </div>
2936 </div>
@@ -33,6 +40,10 @@ export class ImageViewerComponent extends Ion implements OnInit, OnDestroy {
3340 public imageUrl : SafeUrl ;
3441
3542 private dragGesture : PanGesture ;
43+
44+ @ViewChild ( 'imageContainer' ) imageContainer ;
45+ private pinchGesture : Gesture ;
46+ private currentScale : number = 1 ;
3647 private dblClickInAction : boolean ;
3748 public isZoomed : boolean ;
3849
@@ -59,8 +70,18 @@ export class ImageViewerComponent extends Ion implements OnInit, OnDestroy {
5970 this . _zone . runOutsideAngular ( ( ) => this . dragGesture = new ImageViewerGesture ( this . platform , this , this . domCtrl , gestureCallBack ) ) ;
6071 }
6172
73+ ngAfterViewInit ( ) {
74+ // imageContainer is set after the view has been initialized
75+ this . _zone . runOutsideAngular ( ( ) => {
76+ this . pinchGesture = new Gesture ( this . imageContainer . nativeElement ) ;
77+ this . pinchGesture . listen ( ) ;
78+ this . pinchGesture . on ( 'pinch' , this . onPinch ) ;
79+ } ) ;
80+ }
81+
6282 ngOnDestroy ( ) {
6383 this . dragGesture && this . dragGesture . destroy ( ) ;
84+ this . pinchGesture && this . pinchGesture . destroy ( ) ;
6485 }
6586
6687 onImageClick ( ) {
@@ -73,7 +94,16 @@ export class ImageViewerComponent extends Ion implements OnInit, OnDestroy {
7394 }
7495
7596 onImageDblClick ( ) {
97+ // Clear eventual transforms caused by a previous pinch
98+ this . imageContainer . nativeElement . style . transform = '' ;
99+
76100 this . isZoomed = ! this . isZoomed ;
77- this . renderer . setElementClass ( this . getNativeElement ( ) , 'zoom' , this . isZoomed ) ;
101+ this . renderer . setElementClass ( this . imageContainer . nativeElement , 'zoom' , this . isZoomed ) ;
102+ }
103+
104+ onPinch ( event ) {
105+ console . log ( event , this . currentScale )
106+ this . currentScale = Math . max ( MAX_SCALE , this . currentScale * event . scale ) ;
107+ this . imageContainer . nativeElement . style . transform = `scale(${ this . currentScale } )` ;
78108 }
79109}
0 commit comments