ionic slider - lockSwipes(true) disables initialSlide and slideNext() methods?

旧城冷巷雨未停 提交于 2020-12-06 06:03:30

问题


Hi I'm using Ionic Slides to display todo lists and I want to disable the swipe guestures since my list items have them. Instead I want to change the slides via methods.

This is my component:

@ViewChild(Slides) slides: Slides;

days: SbCalendarDay[] = [] //this is a list of todo's of each day

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

slide(direction:string){
    if(direction === 'prev'){
        this.slides.slidePrev()
    }else{
        this.slides.slideNext()
    }
}

And my view:

<div class="sb-calendar-day">
    <ion-slides [initialSlide]="1" (ionSlideDidChange)="slideChanged($event)" (ionSlideWillChange)="slideWillChange($event)">
        <ion-slide *ngFor="let day of days">
            <sb-list [list]="day.events" (sbListEvent)="listEvent($event)"></sb-list>
        </ion-slide>
    </ion-slides>
</div>

However when I lock the swipes, the initialSlide input doesn't work anymore. I'm starting off with an array of 3 days with array[1] being the current day.

Furthermore the methods slideNext() and slidePrev() also don't react. Does lockSwipes(true) completely "lock down" the slider?

Is there a way to only disable the swiping gestures of the slider?

Thanks


回答1:


Yes, by looking at the source code for lockSwipes(), it seems like the functions slideNext and slidePrev gets 'locked':

lockSwipes(shouldLockSwipes: boolean) {
    this._allowSwipeToNext = this._allowSwipeToPrev = !shouldLockSwipes;
}

The function ultimately ends up calling slideTo-function thats being imported from swiper.ts, where a check against _allowSwipeToNext and _allowSwipeToPrev is made:

// Directions locks
if (!s._allowSwipeToNext && translate < s._translate && translate < minTranslate(s)) {
    return false;
}
if (!s._allowSwipeToPrev && translate > s._translate && translate > maxTranslate(s)) {
    if ((s._activeIndex || 0) !== slideIndex ) return false;
}

initialSlide-input does not work is because in the function initSwiper imported from swiper.ts, the value of initialSlide is used as an argument to the function slideTo, and the _allowSwipe[Next|Prev] flags are checked.

For disabling only swipe gestures use onlyExternal:

onlyExternal: If true, then the only way to switch the slide is use of external API functions like slidePrev or slideNext

ngAfterViewInit() {
    this.slides.onlyExternal = true;
}



回答2:


Although I am not sure how good a practice is using setTimeout. But, this hack worked for me.

setTimeout(()=> {
      this.slides.lockSwipes(true);
}, 100);

And template code:

<ion-slides (ionSlideDidChange)="slideChanged($event)" [initialSlide]="1">


来源:https://stackoverflow.com/questions/45516876/ionic-slider-lockswipestrue-disables-initialslide-and-slidenext-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!