应用开发过程中经常会要求用户在textbox进行输入。例如:登陆,发布。

而一般没进行输入的时候我们都会简单的进行弹窗提示用户输入。 前阵子ios的同学搞了一个左右晃动的效果,觉得还不错,于是也搞了个出来。

为方便使用,自定义ShakeTextbox继承TextBox定义晃动、与正常情况的两种状态。

[TemplateVisualState(Name = ShakeTextbox.ShakeVisualState, GroupName = ShakeTextbox.ActivityVisualStateGroup)]
[TemplateVisualState(Name = ShakeTextbox.CommonVisualState, GroupName = ShakeTextbox.ActivityVisualStateGroup)]
public class ShakeTextbox:TextBox
{
private const string ActivityVisualStateGroup = "ActivityStates"; private const string ShakeVisualState = "Shake"; private const string CommonVisualState = "Common"; public void Shake()
{
VisualStateManager.GoToState(this, ShakeTextbox.ShakeVisualState, false);
atimer = new Timer(callback, null, , Timeout.Infinite);
}
Timer atimer;
void callback(object state)
{
Dispatcher.BeginInvoke(() =>
{
VisualStateManager.GoToState(this, ShakeTextbox.CommonVisualState, false);
});
atimer.Dispose();
}
}

app.xaml中写入ShakeTextbox的style。VisualState x:Name="Shake"实现左右晃动的动画效果

<Style TargetType="LocalControl:ShakeTextbox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="LocalControl:ShakeTextbox">
<Grid x:Name="grid" Background="Transparent" RenderTransformOrigin="0.5,0.5">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ActivityStates">
<VisualState x:Name="Shake">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="-9"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="8.5"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="-7.5"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="6"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-4"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1.5"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Common"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.RenderTransform>
<CompositeTransform/>
</Grid.RenderTransform>
<Border x:Name="MainBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}"/>
<Border x:Name="ReadonlyBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed"/>
<Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}">
<ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

最后在页面中使用ShakeTextbox,只要判断ShakeTextbox.Test为空的时候调用ShakeTextbox自带的Shake方法即可。

demo 链接:http://files.cnblogs.com/fatlin/Test.rar

备注:由于本码农的开发工具是vs2013,低版本的vs目测打不开。

自定义textbox加入左右晃动效果的更多相关文章

  1. WPF自定义TextBox及ScrollViewer

    原文:WPF自定义TextBox及ScrollViewer 寒假过完,在家真心什么都做不了,可能年龄大了,再想以前那样能专心坐下来已经不行了.回来第一件事就是改了项目的一个bug,最近又新增了一个新的 ...

  2. 自定义view实现水波纹效果

    水波纹效果: 1.标准正余弦水波纹: 2.非标准圆形液柱水波纹: 虽说都是水波纹,但两者在实现上差异是比较大的,一个通过正余弦函数模拟水波纹效果,另外一个会运用到图像的混合模式(PorterDuffX ...

  3. ios7自带的晃动效果

    ios7自带的晃动效果 by 伍雪颖 - (void)registerEffectForView:(UIView *)aView depth:(CGFloat)depth; { UIInterpola ...

  4. 继承control的自定义TextBox

    继承control的自定义TextBox 下面来介绍一下本人写的一个自定义的textbox,首先说一下写这个控件遇到的几个难点:第一.关联输入法:第二.画字符串和焦点线 先随便上两张效果图吧: 下面这 ...

  5. CSS3左右间歇晃动效果

    今天在做一个活动页面时,产品想要在页面中添加一个吸引人注意的小图片左右晃动的效果,并且该效果是间歇执行的.我一想应该挺简单的吧,二话没说就答应了,谁知在真正实现的时候才发现还是有些许困难的.于是就在网 ...

  6. android 自定义scrollview 仿QQ空间效果 下拉伸缩顶部图片,上拉回弹 上拉滚动顶部title 颜色渐变

    首先要知道  自定义scrollview 仿QQ效果 下拉伸缩放大顶部图片 的原理是监听ontouch事件,在MotionEvent.ACTION_MOVE事件时候,使用不同倍数的系数,重置布局位置[ ...

  7. 自定义modal一个控制器的效果, presentViewController

    presentViewController 一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UI ...

  8. WPF用样式实现TextBox的虚拟提示效果

    [版权声明]本文为博主原创,未经允许禁止用作商业用途,如有转载请注明出处. 话说好多软件和网站都能实现虚拟提示,好吧这个名词是我自己起的,因为我也不知道这么形容这个效果. 效果描述:在TextBox没 ...

  9. WPF 自定义TextBox

    1.TextBox前加图标. 效果: <TextBox Width="300" Height="30" Style="{StaticResour ...

随机推荐

  1. Java再学习——sleep(), wait(), notify(), notifyAll()

    首先一点就是Thread.sleep(long millis)方法是Thread类的静态方法,其他三个wait(), notify()和notifyAll()都是Object类的方法. sleep(l ...

  2. c++ 设计模式9 (Abstract Factory 抽象工厂模式)

    5.2 抽象工厂模式 动机:在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作:同时,由于需求的变化,往往存在更多系列对象的创建工作. 代码示例: 实现利用数据库的业务逻辑,支持多数据库(Sq ...

  3. C# mvc--EF中查询的本质

    UI层我直接用了窗体程序. 好了 不罗嗦 直接上代码…… private void button1_Click(object sender, EventArgs e) { //1.0创建EF上下文容器 ...

  4. 1.7.2 Velocity Search UI

    1. Velocity Search UI solr包含了一个简单的搜索UI界面,是基于VelocityResponseWrite(也被叫做 Solritas)的.这里演示了几个有用的功能,如搜索,分 ...

  5. 1.4.6 其他Schema元素

    这里描述了schema.xml中的其他几个重要的元素. 唯一主键-Unique Key uniqueKey元素一篇文档的唯一标记,它几乎总是保证您的应用程序设计,例如,如果你更新索引中的文档,需要用到 ...

  6. xcode5项目图标添加

    转载文章 地址http://www.360doc.com/content/14/0103/08/14615320_342227413.shtml 最近提交itunesconnect应用时,有个警告说缺 ...

  7. ava SE ---逻辑运算符

      java中有4个逻辑运算符:&与,&& 逻辑与,| 或,|| 逻辑或这些运算符要求操作数和结果值都是布尔型. a&&b   a||b 1) 逻辑与& ...

  8. 【Android Studio使用教程2】Android Studio创建项目

    创建项目 首先,先指出Android Studio中的两个概念. Project 和 Module .在Android Studio中, Project 的真实含义是工作空间, Module 为一个具 ...

  9. 【Android 界面效果16】关于android四大组件的总结

    Android四大组件:Activity.Service.Broadcast receiver.Content provider 在Android中,一个应用程序可以使用其它应用程序的组件,这是And ...

  10. 《Cortex-M0权威指南》之体系结构---程序映像和启动流程

    转载请注明来源:cuixiaolei的技术博客 我们先来看看程序映像. 通常,Cortex-M0处理器的程序映像时从地址0x00000000处开始的. 程序映像开始处时向量表,其中包含了异常的其实地址 ...