简体   繁体   English

钛如何在开关中使用图像

[英]Titanium how to use images in switch

I am new to titanium and am working on switches and I have two images ON and OFF and I when we swipe the ON it should switch to the OFF .. Here are the two images I am trying to use.. 我是钛金属的新手,正在开关上,我有两个图像“ 开”和“ 关” ,当我滑动“ 开”时 ,应该将其切换为“ 关” ..这是我要使用的两个图像。

关闭上

I dont know how to use these images..The only thing I can do is a default switch 我不知道如何使用这些图片。.我唯一能做的就是默认开关

Here is the coding 这是编码

var basicSwitch = Ti.UI.createSwitch({
value:true ,// mandatory property for iOS 
color: 'white',
left : 40,
top:0,
backgroundColor:'black'
});
cview.add(basicSwitch);

basicSwitch.addEventListener('change',function(e){
Ti.API.info('Switch value: ' + basicSwitch.value);

The natively built in Slider does not support custom images (except on iOS). 内置于Slider中的本机不支持自定义图像(iOS上除外)。 I would recommend you to implement this as a button which changes if it is swiped in the desired direction. 我建议您将其实现为一个按钮,如果按所需方向滑动该按钮,该按钮会更改。

var switchButton = Ti.UI.createButton({
    image: "/disabledSwitch.png", //exchange for your location
    //Add the dimensions
    title: "OFF" });

switchButton.addEventListener ('swipe', function() {
    if (e.direction == "right" && e.source.title == "OFF" {
        //Enable the "switch"
        switchButton.setImage("Your active image");
        switchButton.setTitle("ON");
        //Set your desired actions
    } else if (.direction == "left" && e.source.title == "ON" {
        //Disable the "switch"
        switchButton.setImage("Your inactive image");
        switchButton.setTitle("OFF");
        //Set your desired actions
    } });

cview.add(switchButton);

You could also additionally add a click listener. 您还可以另外添加一个点击监听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM