Skip to content

Commit ec99f5b

Browse files
disable pan done
2 parents 7d37f89 + 7704453 commit ec99f5b

File tree

5 files changed

+165
-160
lines changed

5 files changed

+165
-160
lines changed

README.md

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ the `<Slides:SlideContainer>` element also has a property called `loop` which is
7373

7474
the `<Slides:SlideContainer>` element also has a property called `velocityScrolling` which is a boolean value and if set to true will calculate transitions speeds based on the finger movement speed.
7575

76+
the `<Slides:SlideContainer>` element also has a property called `pageIndicators` which will add indicator dots to the bottom of your slides.
77+
7678
the `<Slides:SlideContainer>` element also has a property called `interval` which is a integer value and the value is in milliseconds. The suggested use case would be for a Image Carousel or something of that nature which can change the image for every fixed intervals. In unloaded function call `page.getViewById("your_id").stopSlideshow()` to unregister it (your_id is the id given to `<Slides:SlideContainer>`), it can be restarted with `startSlidShow`.
7779

7880
the `<Slides:SlideContainer>` element also has a property called `disablePan` which is used to disable panning when set to true. So that you can call nextSlide() function to change the slide. If slides is used to get details about users like email, phone number ,username etc in this case you don't want users to move from one slide to another slide without filling details. So, you can disablePan option.
@@ -133,64 +135,6 @@ to
133135
};
134136
```
135137

136-
#### Smoother panning on Android (For {N} v2.0.1 __only__).
137-
138-
To achieve a much smoother drag on android simply go into the gestures.android.js file in the tns-core-modules here
139-
140-
141-
`/node_modules/tns-core-modules/ui/gestures/gestures.android.js`
142-
143-
and change
144-
145-
```javascript
146-
CustomPanGestureDetector.prototype.getEventCoordinates = function (event) {
147-
var count = event.getPointerCount();
148-
if (count === 1) {
149-
return {
150-
x: event.getRawX() / this.density,
151-
y: event.getRawY() / this.density
152-
};
153-
}
154-
else {
155-
var offX = event.getRawX() - event.getX();
156-
var offY = event.getRawY() - event.getY();
157-
var res = { x: 0, y: 0 };
158-
for (var i = 0; i < count; i++) {
159-
res.x += event.getX(i) + offX;
160-
res.y += event.getY(i) + offY;
161-
}
162-
res.x /= (count * this.density);
163-
res.y /= (count * this.density);
164-
return res;
165-
}
166-
};
167-
```
168-
169-
to
170-
```javascript
171-
CustomPanGestureDetector.prototype.getEventCoordinates = function (event) {
172-
var count = event.getPointerCount();
173-
if (count === 1) {
174-
return {
175-
x: event.getRawX() / this.density,
176-
y: event.getRawY() / this.density
177-
};
178-
}
179-
else {
180-
var offX = event.getRawX() - event.getX();
181-
var offY = event.getRawY() - event.getY();
182-
var res = { x: 0, y: 0 };
183-
for (var i = 0; i < count; i++) {
184-
res.x += event.getRawX(i) + offX;
185-
res.y += event.getRawY(i) + offY;
186-
}
187-
res.x /= (count * this.density);
188-
res.y /= (count * this.density);
189-
return res;
190-
}
191-
};
192-
```
193-
194138
_please note this will change the panning gesture for your entire project._
195139

196140

demo/app/main-page.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:Slides="nativescript-slides" loaded="pageLoaded">
22

33
<GridLayout rows="* auto">
4-
<Slides:SlideContainer id="slides" row="0">
4+
<Slides:SlideContainer id="slides" row="0" pageIndicators="true">
55
<Slides:Slide class="slide-1">
66
<Label text="This is Panel 1" />
77
</Slides:Slide>
@@ -19,9 +19,6 @@
1919
</Slides:Slide>
2020
</Slides:SlideContainer>
2121

22-
<StackLayout row="1">
23-
<button tap="next" text="Next"/>
24-
<button tap="prev" text="Prev"/>
25-
</StackLayout>
22+
2623
</GridLayout>
2724
</Page>

nativescript-slides.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { AbsoluteLayout } from 'ui/layouts/absolute-layout';
22
import { StackLayout } from 'ui/layouts/stack-layout';
3+
import { Label } from 'ui/label';
34
export declare class Slide extends StackLayout {
45
}
56
export interface ISlideMap {
67
panel: StackLayout;
8+
index: number;
79
left?: ISlideMap;
810
right?: ISlideMap;
911
}
@@ -21,6 +23,9 @@ export declare class SlideContainer extends AbsoluteLayout {
2123
private timer_reference;
2224
private _angular;
2325
private _disablePan;
26+
private _footer;
27+
private _pageIndicators;
28+
pageIndicators: boolean;
2429
hasNext: boolean;
2530
hasPrevious: boolean;
2631
interval: number;
@@ -33,6 +38,7 @@ export declare class SlideContainer extends AbsoluteLayout {
3338
angular: boolean;
3439
android: any;
3540
ios: any;
41+
currentIndex: number;
3642
constructor();
3743
private setupDefaultValues();
3844
constructView(constructor?: boolean): void;
@@ -47,8 +53,10 @@ export declare class SlideContainer extends AbsoluteLayout {
4753
private applySwipe(pageWidth);
4854
private showRightSlide(panelMap, offset?, endingVelocity?);
4955
private showLeftSlide(panelMap, offset?, endingVelocity?);
50-
private buildFooter();
56+
private buildFooter(pageCount?, activeIndex?);
5157
private setwidthPercent(view, percentage);
5258
private newFooterButton(name);
5359
private buildSlideMap(views);
60+
createIndicator(): Label;
61+
setActivePageIndicator(index: number): void;
5462
}

nativescript-slides.ts

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { Button } from 'ui/button';
77
import {Label} from 'ui/label';
88
import * as AnimationModule from 'ui/animation';
99
import * as gestures from 'ui/gestures';
10-
import {AnimationCurve} from 'ui/enums';
10+
import {AnimationCurve, Orientation} from 'ui/enums';
11+
import {Color} from 'color';
12+
import {Image} from 'ui/image';
13+
import * as imageSource from 'image-source';
1114

1215
let LayoutParams: any;
1316
if (app.android) {
@@ -26,6 +29,7 @@ enum direction {
2629

2730
export interface ISlideMap {
2831
panel: StackLayout;
32+
index: number;
2933
left?: ISlideMap;
3034
right?: ISlideMap;
3135
}
@@ -44,11 +48,20 @@ export class SlideContainer extends AbsoluteLayout {
4448
private timer_reference: number;
4549
private _angular: boolean;
4650
private _disablePan: boolean;
51+
private _footer: StackLayout;
52+
private _pageIndicators: boolean;
53+
54+
/* page indicator stuff*/
55+
get pageIndicators(): boolean {
56+
return this._pageIndicators;
57+
}
58+
set pageIndicators(value: boolean) {
59+
this._pageIndicators = value;
60+
}
4761

4862
get hasNext(): boolean {
4963
return !!this.currentPanel.right;
5064
}
51-
5265
get hasPrevious(): boolean {
5366
return !!this.currentPanel.left;
5467
}
@@ -119,14 +132,17 @@ export class SlideContainer extends AbsoluteLayout {
119132
return;
120133
}
121134

135+
get currentIndex(): number {
136+
return this.currentPanel.index;
137+
}
138+
122139
constructor() {
123140
super();
124141
this.setupDefaultValues();
125142
// if being used in an ng2 app we want to prevent it from excuting the constructView
126143
// until it is called manually in ngAfterViewInit.
127144

128145
this.constructView(true);
129-
130146
}
131147

132148
private setupDefaultValues(): void {
@@ -136,7 +152,6 @@ export class SlideContainer extends AbsoluteLayout {
136152
}
137153

138154
this.transitioning = false;
139-
140155
this._pageWidth = Platform.screen.mainScreen.widthDIPs;
141156

142157
if (this._interval == null) {
@@ -154,6 +169,9 @@ export class SlideContainer extends AbsoluteLayout {
154169
this.angular = false;
155170
}
156171

172+
if (this._pageIndicators == null) {
173+
this._pageIndicators = false;
174+
}
157175
}
158176

159177
public constructView(constructor: boolean = false): void {
@@ -191,6 +209,13 @@ export class SlideContainer extends AbsoluteLayout {
191209
}
192210
});
193211

212+
if (this.pageIndicators) {
213+
this._footer = this.buildFooter(slides.length, 0);
214+
this.addChild(this._footer);
215+
// this.setActivePageIndicator(0);
216+
}
217+
218+
194219
this.currentPanel = this.buildSlideMap(slides);
195220
this.currentPanel.panel.translateX = -this.pageWidth;
196221
if(disablePan == false) {
@@ -209,6 +234,8 @@ export class SlideContainer extends AbsoluteLayout {
209234
if(disablePan == false) {
210235
this.applySwipe(this.pageWidth);
211236
}
237+
let topOffset = Platform.screen.mainScreen.heightDIPs - 105;
238+
AbsoluteLayout.setTop(this._footer, topOffset);
212239
this.currentPanel.panel.translateX = -this.pageWidth;
213240
});
214241
}
@@ -284,6 +311,7 @@ export class SlideContainer extends AbsoluteLayout {
284311
if(disablePan == false) {
285312
this.applySwipe(this.pageWidth);
286313
}
314+
this.setActivePageIndicator(this.currentPanel.index);
287315
this.rebindSlideShow();
288316
}
289317

@@ -447,31 +475,29 @@ export class SlideContainer extends AbsoluteLayout {
447475
/**
448476
* currently deprecated.... will come back to life for navigation dots.
449477
* */
450-
private buildFooter(): AbsoluteLayout {
451-
let footer = new AbsoluteLayout();
478+
private buildFooter(pageCount: number = 5, activeIndex: number = 0): StackLayout {
452479
let footerInnerWrap = new StackLayout();
453-
let footerInnerWrapLeft = new StackLayout();
454-
let footerInnerWrapMiddle = new StackLayout();
455-
let footerInnerWrapRight = new StackLayout();
456-
this.setwidthPercent(footer, 100);
480+
const topOffset = Platform.screen.mainScreen.heightDIPs - 105;
481+
footerInnerWrap.height = 20;
457482

458-
footerInnerWrap.orientation = 'horizontal';
459483
this.setwidthPercent(footerInnerWrap, 100);
484+
AbsoluteLayout.setLeft(footerInnerWrap, 0);
485+
AbsoluteLayout.setTop(footerInnerWrap, topOffset);
460486

461-
footer.addChild(footerInnerWrap);
462-
463-
this.setwidthPercent(footerInnerWrapRight, 30);
464-
this.setwidthPercent(footerInnerWrapLeft, 30);
465-
this.setwidthPercent(footerInnerWrapMiddle, 40);
466-
467-
footerInnerWrapLeft.addChild(this.newFooterButton('Previous'));
468-
footerInnerWrapRight.addChild(this.newFooterButton('Next'));
487+
footerInnerWrap.orientation = 'horizontal';
488+
footerInnerWrap.verticalAlignment = 'top';
489+
footerInnerWrap.horizontalAlignment = 'center';
469490

470-
footerInnerWrap.addChild(footerInnerWrapLeft);
471-
footerInnerWrap.addChild(footerInnerWrapMiddle);
472-
footerInnerWrap.addChild(footerInnerWrapRight);
491+
let i = 0;
492+
while (i < pageCount) {
493+
footerInnerWrap.addChild(this.createIndicator());
494+
i++;
495+
}
473496

474-
return footer;
497+
let activeIndicator = footerInnerWrap.getChildAt(0);
498+
activeIndicator.className = 'slide-indicator-active';
499+
activeIndicator.opacity = 0.9;
500+
return footerInnerWrap;
475501
}
476502

477503
private setwidthPercent(view: View, percentage: number) {
@@ -488,9 +514,10 @@ export class SlideContainer extends AbsoluteLayout {
488514

489515
private buildSlideMap(views: StackLayout[]) {
490516
let slideMap: ISlideMap[] = [];
491-
views.forEach((view: StackLayout) => {
517+
views.forEach((view: StackLayout, index: number) => {
492518
slideMap.push({
493-
panel: view
519+
panel: view,
520+
index: index,
494521
});
495522
});
496523
slideMap.forEach((mapping: ISlideMap, index: number) => {
@@ -507,4 +534,30 @@ export class SlideContainer extends AbsoluteLayout {
507534
this.startSlideshow();
508535
return slideMap[0];
509536
}
537+
538+
createIndicator(): Label {
539+
let indicator = new Label();
540+
indicator.backgroundColor = new Color('#fff');
541+
indicator.opacity = 0.4;
542+
indicator.width = 10;
543+
indicator.height = 10;
544+
indicator.marginLeft = 2.5;
545+
indicator.marginRight = 2.5;
546+
indicator.borderRadius = 5;
547+
return indicator;
548+
}
549+
550+
setActivePageIndicator(index: number) {
551+
552+
this._footer.eachLayoutChild((view: View) => {
553+
if (view instanceof Label) {
554+
view.opacity = 0.4;
555+
view.className = 'slide-indicator-inactive';
556+
}
557+
});
558+
let activeIndicator = this._footer.getChildAt(index);
559+
activeIndicator.className = 'slide-indicator-active';
560+
activeIndicator.opacity = 0.9;
561+
562+
}
510563
}

0 commit comments

Comments
 (0)