Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 - how to prevent page changes on ion-slides by swipe gesture?

I saw the solution in many different sources for this, including Stack Overflow. This is working for me partially: I can't swipe to the left or right to switch the pages. That's fine ! But if the user click a button , keep the button pressed and swipe, this will cause pagination. (I just testing using Android)

The solution according with many sources is that one:

<ion-slides [options]="{onlyExternal: false}">
</ion-slides>

Let me illustrate this with screenshots...

enter image description here

If I swipe here, nothing will happen

Now, if I hold and swipe the red button, this will cause pagination.

enter image description here

like image 530
Marco Jr Avatar asked Apr 05 '17 11:04

Marco Jr


4 Answers

The way i do this is:

On my .ts file of the page i have a slide i do this

import { Component, ViewChild } from '@angular/core';
import { Slides } from 'ionic-angular';

@Component({
  selector: 'page',
  templateUrl: 'page.html'
})
export class Page{
  @ViewChild(Slides) slides: Slides;

  contructor() {
    this.slides.lockSwipes(true);
  }

  nextSlide(){
    this.slides.lockSwipes(false);
    this.slides.slideNext();
    this.slides.lockSwipes(true);
  }
}

And in your HTML you call the function on a button

<button ion-button block (tap)="nextSlide()">NEXT</button>

So when the page is beeing constructed i lock the swipe, and when someone click next/back i unlock the swipe, go to next slide and lock it back.

Hope it helps

like image 186
Gabriel Barreto Avatar answered Oct 18 '22 19:10

Gabriel Barreto


Since externalOnly isn't working in Ionic 4 anymore, i looked into the Swiper API documentation and saw that it changed to allowTouchMove now.

So: <ion-slides [options]="{allowTouchMove:false}"

like image 40
Mathis Schülingkamp Avatar answered Oct 18 '22 19:10

Mathis Schülingkamp


Another way to achieve it is adding the swiper-no-swiping class:

Example:

<ion-slides #slides class="swiper-no-swiping">
...
</ion-slides>
like image 2
Juan Antonio Avatar answered Oct 18 '22 18:10

Juan Antonio


you should use the function ionViewDidLoad() because when you put the "this.slides.lockSwipes(true);" in the constructor, the page havent load yet, try to do this.

ionViewDidLoad(){
    this.slides.lockSwipes(true);
  }
like image 1
Daniel Gomez Avatar answered Oct 18 '22 20:10

Daniel Gomez