应用开发过程中经常会要求用户在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. Linux下判断cpu物理个数、几核

    自己服务器的输出 1. 查看物理CPU的个数   #cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l    1 2. 查 ...

  2. mysql性能优化(二)

    ###> mysql中有一个explain 命令可以用来分析select 语句的运行效果,例如explain可以获得select语句使用的索引情况.排序的情况等等.除此以外,explain 的e ...

  3. jquery冲突

    今天修改一个项目发现,前辈们自己写的一些方法和jquery冲突了,也就是$的冲突,以至于自己用jquery编写的新功能无法正常使用,细究后发现解决办法如下:使用 noConflict() 方法为 jQ ...

  4. 关于禁止Android scrollView 因内容变化而自动滚动 android:descendantFocusability

    出现这种情况是因为你让scrollview获得了焦点,所以它才会滚动.如果你百度了以后,你可能会发现有些博客会说让焦点停在固定的一个view中就可以了.这对于不存在刷新的页面确实是可以的,但是当你出现 ...

  5. git的一些命令

    因为项目的原因,大家把项目托管到git上,然后我不会,队友就传了一个廖雪峰的git教程,讲的很详细,不会用git的同学,可以在http://pan.baidu.com/s/1pKizolP上下载,这是 ...

  6. iOS 手机 邮箱 正则表达式

    //iOS代码//判断邮箱格式是否正确的代码: //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex ...

  7. 11. Android框架和工具之 Logger(调试代码)

    1. Logger Logger是android是一个简单.漂亮.功能强大的Android日志程序. 日志程序提供了 : 线程信息Thread information 类信息Class informa ...

  8. 关于java设计模式与极品飞车游戏的思考

    ------- android培训.java培训.期待与您交流! ---------- 对像我一样正在学习java的人来讲,对设计模式的学习是个很重要的环节.而我们在学习设计模式时,不仅仅应该知道它们 ...

  9. oracle-SQL语言基础-事务控制命令命令

    事务控制命令命令 COMMITROLLBACKSAVEPOINTSET TRANSACTION 当第一条可执行的SQL语句开始执行,数据库事务就开始.随着下面任一事件发生,数据库事务结束:执行COMM ...

  10. 人体时钟hone hone clock

    摘要:一个由日本人设计的有意思的Flash时钟:人体时钟 hone hone clock .安装很简单,直接js导入即可,包括两种样式:透明背景和白色背景. 很可爱的一个设计,实现后效果如下: 使用方 ...