这两天在网上看到一个帖子讨论关于有些app 输入账密时候 错误的话会有抖动效果出现,然后自己琢磨了下如何实现,下面上代码!!!

首先 写一个UIView的分类

 #import <UIKit/UIKit.h>

 typedef NS_ENUM(NSInteger, QHLDirection) {
QHLDirectionHorizontal,
QHLDirectionVertical
};
@interface UIView (QHLShakes)
- (void)shakeWithShakeDirection:(QHLDirection)shakeDirection;
- (void)shakeWithTimes:(NSInteger)times shakeDirection:(QHLDirection)shakeDirection;
- (void)shakeWithTimes:(NSInteger)times speed:(CGFloat)speed shakeDirection:(QHLDirection)shakeDirection;
- (void)shakeWithTimes:(NSInteger)times speed:(CGFloat)speed range:(CGFloat)range shakeDirection:(QHLDirection)shakeDirection;
@end #import "UIView+QHLShakes.h" @implementation UIView (QHLShakes)
- (void)shakeWithShakeDirection:(QHLDirection)shakeDirection {
[self shakeWithTimes: speed:0.05 range: shakeDirection:shakeDirection];
} - (void)shakeWithTimes:(NSInteger)times shakeDirection:(QHLDirection)shakeDirection {
[self shakeWithTimes:times speed:0.05 range: shakeDirection:shakeDirection];
} - (void)shakeWithTimes:(NSInteger)times speed:(CGFloat)speed shakeDirection:(QHLDirection)shakeDirection {
[self shakeWithTimes:times speed:speed range: shakeDirection:shakeDirection];
} - (void)shakeWithTimes:(NSInteger)times speed:(CGFloat)speed range:(CGFloat)range shakeDirection:(QHLDirection)shakeDirection {
[self viewShakesWithTiems:times speed:speed range:range shakeDirection:shakeDirection currentTimes: direction:];
}
/**
* @param times 震动的次数
* @param speed 震动的速度
* @param range 震动的幅度
* @param shakeDirection 哪个方向上的震动
* @param currentTimes 当前的震动次数
* @param direction 向哪边震动
*/
- (void)viewShakesWithTiems:(NSInteger)times speed:(CGFloat)speed range:(CGFloat)range shakeDirection:(QHLDirection)shakeDirection currentTimes:(NSInteger)currentTimes direction:(int)direction{ [UIView animateWithDuration:speed animations:^{
self.transform = (shakeDirection == QHLDirectionHorizontal)? CGAffineTransformMakeTranslation(range * direction, ):CGAffineTransformMakeTranslation(, range * direction);
} completion:^(BOOL finished) {
if (currentTimes >= times) {
[UIView animateWithDuration:speed animations:^{
self.transform = CGAffineTransformIdentity;
}];
return;
}
#pragma mark - 循环到times == currentTimes时候 会跳出该方法
[self viewShakesWithTiems:times -
speed:speed
range:range
shakeDirection:shakeDirection
currentTimes:currentTimes +
direction:direction * -];
}];
}
@en
然后在ViewController.m中

先导入头文件 #import "UIView+QHLShakes.h"
 #import "ViewController.h"
#import "UIView+QHLShakes.h" #define QHLFont [UIFont boldSystemFontOfSize:17]
#define QHLColor [UIColor purpleColor]
#define QHLCGColor [QHLColor CGColor] @interface ViewController ()
@property (nonatomic, strong) UITextField *show;
@property (nonatomic, strong) UISegmentedControl *directBtn;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self setUpShowTextField];
[self setUpBtn];
} #pragma mark - show
- (void)setUpShowTextField {
UITextField *show = [[UITextField alloc] init];
show.frame = CGRectMake(, , , );
show.textAlignment = NSTextAlignmentCenter;
show.text = @"你是猪吗?";
show.textColor = QHLColor;
show.layer.cornerRadius = ;
show.layer.masksToBounds = YES;
show.layer.borderWidth = 2.0;
show.layer.borderColor = QHLCGColor;
self.show = show;
[self.view addSubview:show];
}
#pragma mark - btn
- (void)setUpBtn {
UIButton *btn = [[UIButton alloc] init];
btn.layer.borderColor = QHLCGColor;
btn.frame = CGRectMake(, , , );
btn.layer.borderWidth = 2.0;
btn.layer.cornerRadius = ;
btn.layer.masksToBounds = YES;
[btn setTitle:@"点我呀" forState:UIControlStateNormal];
[btn setTitle:@"猪是你" forState:UIControlStateHighlighted];
[btn setTitleColor:QHLColor forState:UIControlStateNormal];
[self.view addSubview:btn]; [btn addTarget:self action:@selector(btnDidClick) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - btn 点击事件
- (void)btnDidClick {
[self.show shakeWithTimes: speed:0.05 range: shakeDirection:(self.directBtn.selectedSegmentIndex == )?QHLDirectionHorizontal:QHLDirectionVertical];
}
@end

在 - (void)viewDidLoad {} 中添加一个textField和button,然后设置相关的属性,并给button添加点击事件

当点击事件触发的时候,textField抖动!!!!

自己试了textField 和button的抖动效果 别的没试~~~

如果哪里有些错的地方 求大神指点!!!

IOS中对于一些控件的抖动效果的更多相关文章

  1. Unity3d IOS中的IGUI控件

    Unity3d IOS中的IGUI控件 @灰太龙  群63438968 我讲一下IOS中用的UI,我们采用IGUI,需要使用IGUI的高版本,在Unity3d 4.2中也可以使用的! 之前IGUI有个 ...

  2. IOS中调整UI控件位置和尺寸

    1.frame(修改位置和尺寸):以父控件左上角为坐标原点,在其父控件中的位置和尺寸. //frame属性中的坐标点不能直接修改 CGRect tempFrame = self.v.frame; // ...

  3. ios 中的UI控件学习总结(1)

    UIKit框架提供了非常多功能强大又易用的UI控件 下面列举一些在开发中可能用得上的UI控件 UIButton 按钮 UILabel 文本标签 UITextField 文本输入框 UIImageVie ...

  4. 【转】IOS中各种常用控件的默认高度,很全

    1.状态栏 状态栏一般高度为20像素,在打手机或者显示消息时会放大到40像素高,注意,两倍高度的状态栏在好像只能在纵向的模式下使用.如下图   用户可以隐藏状态栏,也可以将状态栏设置为灰色,黑色或者半 ...

  5. ios中VRGCalendarView日历控件

    http://pan.baidu.com/share/link?shareid=4166002480&uk=923776187 官网 https://github.com/TjeerdVuri ...

  6. 【Animation】 使用handler和Runnable实现某一个控件的抖动效果

    布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool ...

  7. ios开发中关闭textview控件的虚拟键盘

    在ios开发中,textfield控件在点击的时候出现虚拟键盘,关掉虚拟键盘可以通过虚拟键盘中的done button和点击view中的任意地方来关闭虚拟键盘. 1.第一种方法是textfield控件 ...

  8. Xamarin iOS教程之页面控件

    Xamarin iOS教程之页面控件 Xamarin iOS 页面控件 在iPhone手机的主界面中,经常会看到一排小白点,那就是页面控件,如图2.44所示.它是由小白点和滚动视图组成,可以用来控制翻 ...

  9. 在DevExpress程序中使用TeeList控件以及节点查询的处理

    在很多情况下,我们需要通过树列表进行数据的展示,如一些有层次关系的数据,通过有层级的展示,能够使用户更加直观查看和管理相关的数据.在一般Winform开发的情况下,可以使用微软的TreeView控件, ...

随机推荐

  1. spring 学习 AOP和IOC

    自11开始接触三大框架,至今已俞5载, 当时风光无限的ssh,现在还在被广泛使用,并有扩大之势的只有spring了 spring主要特性,是广为使用的AOP(面向切面)和IOC(控制反转) 1.其中, ...

  2. 使用SQL除掉文本中特殊的ascll字符比如Enter,Tab,空格键

    一.在SQL查询的字段中如果包含tab.enter.空格键,可以使用ascii码进行替换: --替换了文本中含有tab键,Enter键,空格键的ascii码 select REPLACE(REPLAC ...

  3. 关于curl_setopt参数的记录

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE) 其中true输出执行结果,false为不输出 fsockopen与curl_setopt请求的区别之一就是 ...

  4. JNI学习&使用过程中的错误

    Part 1 Ubuntu下JNI的简单使用: http://blog.csdn.net/fengqiaoyebo2008/article/details/6210499 Part 2 在eclips ...

  5. Robot Framework自动化测试---元素定位

    不要误认为Robot framework 只是个web UI测试工具,更正确的理解Robot framework是个测试框架,之所以可以拿来做web UI层的自动化是国为我们加入了selenium2的 ...

  6. OC高级编程——深入block,如何捕获变量,如何存储在堆上

    OC高级编程——深入block,如何捕获变量,如何存储在堆上   首先先看几道block相关的题目 这是一篇比较长的  博文 ,前部分是block的测试题目,中间是block的语法.特性,block讲 ...

  7. bindingredirect 没有效果

    在搞在线聊天室的时候用到了SignalR 1.1.4,依赖于Newtonsoft.Json 4.5.0.0. 而我另外的dll又依赖Newtonsoft.Json 6.0.0.0 我只引用6.0.0. ...

  8. hdu Big Number

    #include <cstdio> #include <cstring> #include <cmath> using namespace std; int mai ...

  9. How can I let the compiled script depend on something dynamic

    Compile your script with /DNAME=value or /X"nsis command" passed on to makensis.exe as com ...

  10. iptables 顺序

    -A INPUT -s 115.236.6.6/32 -p udp -m udp --dport 111 -j ACCEPT -A INPUT -s 10.175.197.98/32 -p udp - ...