创建自定义风格的开关控件Custom switch

Fuse拥有一个很强大和灵活的系统来帮助你设计你自有的控件,让我们一起看一下设计一个ToggleControl控件是如何操作的。

contact2

在这个示例中我们将用到:

  • 构建我们自己的控件用的是ToggleControl的子类,拥有多种状态(�此例中是on和off);
  • 一个时间轴Timeline允许基于同样的参数进行多重交互,在此例中的开关拨弄switch thumb便是;allow multiple interactions to work on the same parameter seamlessly。
  • 充许用户基于控件来滑动拇指,使用的是手势SwipeGesture或者点击控件来开关状态;

设计一个样式开关,我们定位到的最主要的事情如下:

  • 控制器需要有一个边界,当开关为On变白的时候;The control should have a border that turns white when it is On
  • 当控件转到On的时候,轨迹形状的背景应该是透明的;The background of the track should turn transparent as the control is turned On

在这里我们简要回顾下ux:Name和ux:Class及内联类�等章节:

  • ux:Name
  • 如何在Fuse中建立自定义的UI组件

定制开关switch的代码很简单:

<pre>
<ToggleControl ux:Class="MySwitch" Margin="4,4,4,4" Width="80" Height="48" Focus.IsFocusable="true" ux:Name="_switch">
<Clicked>
<ToggleSwipeActive Target="swipe" />
</Clicked>

<SwipeGesture Direction="Right" Length="38" Type="Active" ux:Name="swipe" />

<WhileSwipeActive Source="swipe">
    <Change Target="_switch.Value" Value="true" />
</WhileSwipeActive>
<SwipingAnimation Source="swipe">
    <Change enabledTimeline.TargetProgress="1" />
</SwipingAnimation>

<Panel Layer="Background">
    <Circle Alignment="CenterLeft"  Width="34" Height="34" Margin="4,0,14,0">
        <SolidColor ux:Name="thumbFill" Color="#fafafaff" />
        <DropShadow Angle="90" Distance="0" Size="2" Spread="0.05" />
        <Timeline ux:Name="enabledTimeline">
            <Move X="38" Duration="0.25" Easing="QuadraticInOut" />
        </Timeline>
    </Circle>

    <Rectangle CornerRadius="23" Width="80" Height="40" Alignment="Center">
        <SolidColor ux:Name="trackFill" Color="#e7e7e7" />
        <Stroke>
            <SolidColor ux:Name="strokeColor" Color="#ffffff00" />
        </Stroke>
    </Rectangle>
</Panel>

<WhileDisabled>
    <Change thumbFill.Color="#bdbdbdff" Easing="QuadraticInOut" Duration="0.25" />
    <Change trackFill.Color="#0000001e" Easing="QuadraticInOut" Duration="0.25" />
</WhileDisabled>

<WhileTrue>
    <Change thumbFill.Color="#fff" Easing="QuadraticInOut" Duration="0.25" />
    <Change trackFill.Color="#ffffff00" Easing="QuadraticInOut" Duration="0.25" />
    <Change strokeColor.Color="#ffffffff" Easing="QuadraticInOut" Duration="0.25" />
    <Change enabledTimeline.TargetProgress="1" DelayBack="0" />
</WhileTrue>

</ToggleControl>
</pre>

这对于设计一个开关控件ToggleControl来说不过是最基本的事情了,先完成静止状态的效果绘制,然后当我们响应ToggleControl控件的状态改变的时候,让我们一起来看看一些有意思的地方。

<pre>
<Clicked>
<ToggleSwipeActive Target="swipe" />
</Clicked>
</pre>

如你所见,我们改变滑动手势的状态,用来直接代替ToggleControl的状态改变,这使得我们对保持不同的交互同时步发生时变得非常简单。 we change the state of the swipe gesture. This makes it easier for us to keep the different interactions in sync.

<pre>
<SwipeGesture Direction="Right" Length="38" Type="Active" ux:Name="swipe" />

<WhileSwipeActive Source="swipe">
<Change Target="_switch.Value" Value="true" />
</WhileSwipeActive>
<SwipingAnimation Source="swipe">
<Change enabledTimeline.TargetProgress="1" />
</SwipingAnimation>
</pre>

当开关被触动的过程当中,我们首先使用相同的�长度length添加到滑动手势SwipeGesture中。
然后,我们改变ToggleControl的值Value,作为SwipeGesture处于活跃状态Active-state时的一个结果。
最终,我们引用到触动时的时间轴timeline来按比例地移动他,这里的时间轴Timeline是定义在双向开关switch内部的。

<pre>
<Circle
Alignment="CenterLeft"
Width="34"
Height="34"
Margin="4,0,14,0">
<SolidColor ux:Name="thumbFill" Color="#fafafaff" />
<DropShadow Angle="90" Distance="0" Size="2" Spread="0.05" />
<Timeline ux:Name="enabledTimeline">
<Move X="38" Duration="0.25" Easing="QuadraticInOut" />
</Timeline>
</Circle>
</pre>

我们接着来改变这里的时间轴,在ToggleControl是启用状态的时候:

<pre>
<WhileTrue>
<Change thumbFill.Color="#fff" Easing="QuadraticInOut" Duration="0.25" />
<Change trackFill.Color="#ffffff00" Easing="QuadraticInOut" Duration="0.25" />
<Change strokeColor.Color="#ffffffff" Easing="QuadraticInOut" Duration="0.25" />
<Change enabledTimeline.TargetProgress="1" DelayBack="0" />
</WhileTrue>
</pre>

The surrounding areas

In the areas surrounding the switch, we want to:

Create a Circle that expands and contracts as the switch is toggled
Subtly change the colors of the icons
Change the TextColor of the Header (inline class)
Change the SolidColor in the background when the Circle has expanded to its maximum value
Make sure the expanding Circle never extends outside its containing DockPanel, this is achieved using ClipToBounds="true"
Slightly move the Circle as the switch changes state, so it expands from and contracts to slightly different locations

代码区

我们使用的是内联类inline classes(注:这里的的内联类跟新建类时的内部类ux:InnerClass是两回事哦!
<pre>
<Text ux:Class="Header" FontSize="24" TextColor="#11abfe" TextAlignment="Center" Margin="0,35,0,5" />
<Text ux:Class="Description" TextWrapping="Wrap" FontSize="14" TextAlignment="Center" Margin="35,10,35,5" />
<Image ux:Class="Icon" Alignment="VerticalCenter" Height="80" Width="80" Color="#dedede" />
</pre>

然后,配置入口就只有一点点事情了:
<pre>
<DockPanel ClipToBounds="true">
<Rectangle Layer="Background">
<SolidColor ux:Name="secondBackgroundColor" Color="#fff" />
</Rectangle>
<Header Dock="Top" ux:Name="secondHeader">Automatic synchronization</Header>
<Description Dock="Top">Synchronize all contact information when connecting using USB.</Description>
<StackPanel Orientation="Horizontal" Alignment="Center">
<Icon Height="80" File="Assets/Connect.png" ux:Name="connect" />
<Panel Margin="35,0,0,0">
<MySwitch Alignment="VerticalCenter">
<WhileTrue>
<Change greenScaling.Factor="7" Duration="0.25" Easing="QuadraticOut" Delay="0.20" />
<Change secondHeader.TextColor="#fff" Duration="0.25" Delay="0.20" />
<Change connect.Color="#fff" Duration="0.25" Delay="0.35" />
<Change greenColor.Color="#8cb542" Duration="0.25" Easing="QuadraticOut" Delay="0.20" />
<Change secondBackgroundColor.Color="#8cb542" Duration="0.05" Delay="0.35" />
<Change greenCircleTranslation.X="19" Duration="0" DurationBack="0" Delay="0.25" Easing="QuadraticInOut"/>
</WhileTrue>
</MySwitch>
<Circle>
<Translation ux:Name="greenCircleTranslation" X="-19" />
<SolidColor ux:Name="greenColor" Color="#ffffffff" />
<Scaling ux:Name="greenScaling" Factor="0" />
</Circle>
</Panel>
</StackPanel>
</DockPanel>
</pre>

Tag:Fuse, Fuseapp, Fusetools, native app
发布时间:2016年05月08日
博客被黑,挪窝简书安家……

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容