这两天在网上看到一个帖子讨论关于有些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 的ioc 知识点

    1. spring ioc的优点    解耦,对象的创建不直接在代码里生成(不方便修改) ,交由容器创建.... 2.IOC 和 DI 的区别        依赖关系:A类 里 要调用B类  如 se ...

  2. 第1章 网络编程基础(3)——基本Socket通信

    服务器常用模型

  3. Sed命令学习

    1.Sed简介     流数据编辑器 Stream editer(sed),它是一种行编辑器(对应于全屏编辑器),一次处理一行的内容.默认不编辑原文件内容(-i会直接修改原文件).处理时,它先将当前符 ...

  4. js的一个稍微高级点的用法

    通过问题来说明: 1.一个系统中,要创造很多对象,为每个对象分配一个唯一的ID 1: var createObj = (function(){ 2: var i = 0; 3: return func ...

  5. PclZip:强大的PHP压缩与解压缩zip类

    PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip.PKZIP):且能对此类类档案进行处理,包括产 ...

  6. 获取当前页面的完整URL

    PHP实现 #测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."< ...

  7. Python新手学习基础之运算符——位运算

    位运算符 位运算实际上是把数字看作二进制来进行计算,它的运算法则如下: 结合实例,来看下位运算是如何进行的吧: 位运算在实际应用中用途很广泛,比如我们经常听到的子网掩码,它其实就是和IP地址做了按位与 ...

  8. 【好程序员笔记分享】——iOS开发之使用TextField作为搜索框

    -iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之使用TextField作为搜索框     今天给大家带来一个新的技巧,比如平时我们要使用代码创建一 ...

  9. SDWebImage使用——一个可管理远程图片加载的类库

    SDWebImage使用——一个可管理远程图片加载的类库     SDWebImage使用——一个可管理远程图片加载的类库 SDWebImage托管在github上.https://github.co ...

  10. Android Studio 设置LogCat 颜色

    1. File -> Settings ->Search LogCat 2. Save a schema first, for instance: MyLogCatStyle 3. cho ...