-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTiUIScrollableView_child_event.js
109 lines (98 loc) · 2.32 KB
/
TiUIScrollableView_child_event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Issue:
*
* A TiUIScrollableView consisting of multiple children views associated with it,
* may get multiple events fired i.e. click & swipe when attempt to scroll either left or right.
*
* On children views, we generally take care of a few things:
*
* 1. Require to disable captures all touches (even swipe ones) as clicks
* 2. We would like to be able to click a child-view and still be able to scroll between the views.
*
* How to mitigate:
*
* Instead of click event listener, touchStart & touchEnd events
* overide swipe to capture gesture accurate.
*
* Tested:
*
* Titanium Mobile SDK 3.0.2 - iOS 6 simulator & Android 2.3.5
*
*/
//helper variables
var x;
var y;
// ==========
// =Function: getView
// =Params: iteration, bgColor
// ==========
function getView(i, color) {
var view = Ti.UI.createView({
backgroundColor : color
});
view.add(
Ti.UI.createLabel({
text : 'View ' + i,
color : '#fff',
width : Ti.UI.SIZE,
height : Ti.UI.SIZE
})//TiUILabel
);
/*
//Common approach causes events get wrongly handled.
view.addEventListener('click', function(e) {
Ti.API.info("view "+ i + " clicked!");
});
view.addEventListener('swipe', function(e) {
Ti.API.info("view "+ i + " swipe!");
});
*/
//Instead, touchStart & touchEnd events overide swipe and
//capture gesture accurately
view.addEventListener('touchstart', function(e) {
x = e.x;
y = e.y;
});
view.addEventListener('touchend', function(e) {
if (x == e.x && y == e.y) {
Ti.API.info('view ' + i + ' Click event');
}
});
view.addEventListener('swipe', function(e) {
Ti.API.info('view ' + i + ' Swipe event');
});
return view;
}
//Building a simple UI
var win1 = Titanium.UI.createWindow({
backgroundColor : '#fff'
});
win1.add(
Titanium.UI.createLabel({
color : '#999',
text : 'I am Window 1',
font : {
fontSize : 20,
fontFamily : 'Helvetica Neue'
},
textAlign : 'center',
width : 'auto'
})
);
var viewsArray = [], i;
for ( i = 0; i <= 4; i++) {
var bgColor = "purple";
if (i & 1) {
bgColor = "blue";
}
viewsArray[i] = getView(i, bgColor);
}
var scrollView = Titanium.UI.createScrollableView({
views : viewsArray,
showPagingControl : true,
pagingControlHeight : 30,
maxZoomScale : 2.0,
currentPage : 1
});
win1.add(scrollView);
win1.open();