This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
sample_code:img_toggle_button [2014/11/26 09:41] stevegarman mention OnTouch callbacks |
sample_code:img_toggle_button [2015/01/13 09:55] stevegarman [The code] fix code formatting |
||
---|---|---|---|
Line 7: | Line 7: | ||
The sample code passes one of each. | The sample code passes one of each. | ||
- | In case it is not obvious, the actual toggling is done in the OnTouchUp | + | The first image decides the size of the button. |
+ | |||
+ | In case it is not obvious, the actual toggling is done in the OnTouchDown | ||
You could move this to any of the OnTouch events but be aware that some of them (such as SetOnTouch) may fire more often than you expect. | You could move this to any of the OnTouch events but be aware that some of them (such as SetOnTouch) may fire more often than you expect. | ||
Line 13: | Line 15: | ||
<code javascript toggle.js> | <code javascript toggle.js> | ||
// | // | ||
- | function OnStart(){ | + | function OnStart() |
- | // | + | { |
- | lay = app.CreateLayout( " | + | |
- | + | | |
- | // | + | |
- | var pauseImg = app.CreateImage( "/ | + | var pauseImg |
- | + | | |
- | // | + | |
- | btn = CreateImageToggle(pauseImg, | + | btn.SetOnTouchDown(btnToggle); |
- | btn.SetOnTouchDown(btnToggle); | + | lay.AddChild( btn ); |
- | + | | |
- | lay.AddChild( btn ); | + | |
- | + | ||
- | // | + | |
- | app.AddLayout( lay ); | + | |
} | } | ||
- | function btnToggle(ev){ | + | function btnToggle(ev) |
- | this.Toggle = ! this.Toggle; | + | { |
- | if(this.Toggle){ | + | |
- | this.SetImage(this.imgTrue); | + | |
- | app.ShowPopup(" | + | { |
- | } else{ | + | |
- | this.SetImage(this.imgFalse); | + | |
- | app.ShowPopup(" | + | |
- | } | + | |
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
} | } | ||
- | function CreateImageToggle(img1, | + | function CreateImageToggle(img1, img2) |
- | // | + | { // |
- | // | + | |
- | if (typeof img1 == " | + | |
- | img1 = app.CreateImage(img1); | + | |
- | } | + | { |
- | if (typeof img2 == " | + | |
- | img2 = img1; | + | } |
- | } else if (typeof img2 == " | + | |
- | img2 = app.CreateImage(img2, | + | { |
- | } | + | |
- | var obj = app.CreateImage(null, | + | |
- | obj.SetImage(img1) | + | else if (typeof img2 |
- | obj.Toggle = false; | + | { |
- | obj.imgFalse = img1 | + | |
- | obj.imgTrue = img2 | + | } |
- | return obj | + | var obj = app.CreateImage(null, |
+ | | ||
+ | obj.Toggle = false; | ||
+ | | ||
+ | obj.imgTrue = img2; | ||
+ | return obj; | ||
} | } | ||
- | |||
</ | </ | ||
===== Note ===== | ===== Note ===== | ||
This code has had only rudimentary testing. You may well be able to improve it | This code has had only rudimentary testing. You may well be able to improve it | ||