1つの画像を含むページがありますが、この画像をズーム可能にするにはどうすればよいですか?
画像ビューアはIonic 2+
npm install --save ionic-img-viewer
import { IonicImageViewerModule } from 'ionic-img-viewer'; @NgModule({ declarations: [ MyApp, HomePage ], imports: [ BrowserModule, IonicModule.forRoot(MyApp), IonicImageViewerModule ],
<img src = "IMAGE_URL" #myImage(click)= "presentImage(myImage)" />
import { ImageViewerController } from 'ionic-img-viewer'; export class MyPage { _imageViewerCtrl: ImageViewerController; constructor(imageViewerCtrl: ImageViewerController) { this._imageViewerCtrl = imageViewerCtrl; } presentImage(myImage) { const imageViewer = this._imageViewerCtrl.create(myImage); imageViewer.present(); } }
この回避策はIonicフォーラム:
html:
<ion-scroll (pinch)="pinchEvent($event)" scrollX="true" scrollY="true" zoom="true" style="width:100%;height:100%;text-align:center;">
<div [ngStyle]="{'background':'url('+src+') no-repeat','width' : width + 'px', 'height' : height + 'px', 'transform' : 'rotate('+rotacion+'deg)', 'background-size' : 'contain', 'background-position' : 'center'}" style="margin-top:50px;margin-bottom:50px;"></div>
</ion-scroll>
.tsメソッド:
pinchEvent(e) {
this.width = this.pinchW * e.scale;
this.height = this.pinchH * e.scale;
if(this.timeout == null){
this.timeout = setTimeout(() => {
this.timeout = null;
this.updateWidthHeightPinch();
}, 1000);
} else {
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.timeout = null;
this.updateWidthHeightPinch();
}, 1000);
}
}
updateWidthHeightPinch() {
this.pinchW = this.width;
this.pinchH = this.height;
}
私は「ピンチ」イベントを自分でコーディングします-それは非常に簡単でした(建物のフロアマップをズームするために使用します):
_div class="floor-map"
style="position: relative; left: 0px; top: 0px; height: 0px; overflow: hidden; cursor: move;"
(touchstart)="mapMoveStart($event)"
(touchend)="mapMoveStop($event)"
(touchmove)="mapMove($event)"
tappable
#floorMapRef
>
<img [style.width]="width + 'px'"
[style.height]="height + 'px'" ...>
</div>
_
そしてmapMoveには次のようなものがあります:
_pinchLenght = -1 // -1 means no pinch
...
mapMoveStart(event) {
this.pinchLenght = -1; // "reset" pinch
...
}
mapMove(event)
{
if(event.touches.length==2) { // two fingers detection
let length = Math.abs(event.touches[0].clientX-event.touches[1].clientX)
+ Math.abs(event.touches[0].clientY-event.touches[1].clientY);
if(this.pinchLenght < 0) { // case: first pinch "touch"
this.pinchLenght = length
} else {
let delta = length - this.pinchLenght;
this.pinchLenght = length;
if(delta>0) {
this.zoomMap(1.05);
}
if(delta<0) {
this.zoomMap(0.95);
}
}
}
...
}
...
_
そして、メソッドzoomMap(zoomFactor: number)
で、_this.height
_と_this.width
_(これは_<img>
_タグに「バインド」されていました)を適切に変更します。