-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslideLock.js
90 lines (73 loc) · 2.54 KB
/
slideLock.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
/* Sample Usage in app.js:
var win = Ti.UI.createWindow({
backgroundColor: "#333"
});
win.open();
var slideLock = require('slideLock');
var myLock = slideLock({
button:{
size:40,
color:"blue",
center:{x:50,y:50}
},
lock:{
size:100,
color:"red",
center:{x:Ti.Platform.displayCaps.platformWidth/2, y:Ti.Platform.displayCaps.platformHeight/2},
unlockMessage:"You have unlocked your phone!"
},
view:{
obj:win,
selectedColor:"green"
}
});
*/
function slideLock(args){
var lockView = args.view.obj;
var backgroundColor = lockView.backgroundColor
this.lock = Ti.UI.createView({
height:args.lock.size,
width:args.lock.size,
borderRadius:args.lock.size/2,
borderColor:args.lock.color,
backgroundColor:"transparent",
borderWidth:Math.floor(args.lock.size*.05),
center:args.lock.center,
locked:true
});
lockView.add(this.lock);
this.button = Ti.UI.createView({
backgroundColor:args.button.color,
center:args.button.center,
height:args.button.size,
width:args.button.size,
borderRadius:args.button.size/2,
borderColor:args.button.color,
borderWidth:Math.floor(args.button.size*.05)
});
lockView.add(button);
button.addEventListener('touchmove', buttonTouchmove);
button.addEventListener('touchend', buttonTouchend);
function buttonTouchmove(evt) {
var point = button.convertPointToView({ x: evt.x, y: evt.y }, lockView);
button.center = point;
if(Math.pow((point.x - lock.center.x),2)+(Math.pow((point.y - lock.center.y),2))<=(Math.pow(lock.borderRadius, 2))){
lock.locked = false;
lock.backgroundColor = args.lock.color;
lockView.backgroundColor = args.view.selectedColor;
button.backgroundColor = "transparent";
} else {
lock.locked = true;
lock.backgroundColor = "transparent"
lockView.backgroundColor = backgroundColor;
button.backgroundColor = args.button.color;
}
};
function buttonTouchend(evt){
var point = button.convertPointToView({ x: evt.x, y: evt.y }, lockView);
if(!lock.locked){
alert(args.lock.unlockMessage);
};
};
}
module.exports = slideLock;