这两天在网上看到一个帖子讨论关于有些app 输入账密时候 错误的话会有抖动效果出现,然后自己琢磨了下如何实现,下面上代码!!!
首先 写一个UIView的分类
1 #import <UIKit/UIKit.h> 2 3 typedef NS_ENUM(NSInteger, QHLDirection) { 4 QHLDirectionHorizontal, 5 QHLDirectionVertical 6 }; 7 @interface UIView (QHLShakes) 8 - (void)shakeWithShakeDirection:(QHLDirection)shakeDirection; 9 - (void)shakeWithTimes:(NSInteger)times shakeDirection:(QHLDirection)shakeDirection; 10 - (void)shakeWithTimes:(NSInteger)times speed:(CGFloat)speed shakeDirection:(QHLDirection)shakeDirection; 11 - (void)shakeWithTimes:(NSInteger)times speed:(CGFloat)speed range:(CGFloat)range shakeDirection:(QHLDirection)shakeDirection; 12 @end 13 14 15 16 #import "UIView+QHLShakes.h" 17 18 @implementation UIView (QHLShakes) 19 - (void)shakeWithShakeDirection:(QHLDirection)shakeDirection { 20 [self shakeWithTimes:10 speed:0.05 range:5 shakeDirection:shakeDirection]; 21 } 22 23 - (void)shakeWithTimes:(NSInteger)times shakeDirection:(QHLDirection)shakeDirection { 24 [self shakeWithTimes:times speed:0.05 range:5 shakeDirection:shakeDirection]; 25 } 26 27 - (void)shakeWithTimes:(NSInteger)times speed:(CGFloat)speed shakeDirection:(QHLDirection)shakeDirection { 28 [self shakeWithTimes:times speed:speed range:5 shakeDirection:shakeDirection]; 29 } 30 31 - (void)shakeWithTimes:(NSInteger)times speed:(CGFloat)speed range:(CGFloat)range shakeDirection:(QHLDirection)shakeDirection { 32 [self viewShakesWithTiems:times speed:speed range:range shakeDirection:shakeDirection currentTimes:0 direction:1]; 33 } 34 /** 35 * @param times 震动的次数 36 * @param speed 震动的速度 37 * @param range 震动的幅度 38 * @param shakeDirection 哪个方向上的震动 39 * @param currentTimes 当前的震动次数 40 * @param direction 向哪边震动 41 */ 42 - (void)viewShakesWithTiems:(NSInteger)times speed:(CGFloat)speed range:(CGFloat)range shakeDirection:(QHLDirection)shakeDirection currentTimes:(NSInteger)currentTimes direction:(int)direction{ 43 44 [UIView animateWithDuration:speed animations:^{ 45 self.transform = (shakeDirection == QHLDirectionHorizontal)? CGAffineTransformMakeTranslation(range * direction, 0):CGAffineTransformMakeTranslation(0, range * direction); 46 } completion:^(BOOL finished) { 47 if (currentTimes >= times) { 48 [UIView animateWithDuration:speed animations:^{ 49 self.transform = CGAffineTransformIdentity; 50 }]; 51 return; 52 } 53 #pragma mark - 循环到times == currentTimes时候 会跳出该方法 54 [self viewShakesWithTiems:times - 1 55 speed:speed 56 range:range 57 shakeDirection:shakeDirection 58 currentTimes:currentTimes + 1 59 direction:direction * -1]; 60 }]; 61 } 62 @en
然后在ViewController.m中
先导入头文件 #import "UIView+QHLShakes.h"
1 #import "ViewController.h" 2 #import "UIView+QHLShakes.h" 3 4 #define QHLFont [UIFont boldSystemFontOfSize:17] 5 #define QHLColor [UIColor purpleColor] 6 #define QHLCGColor [QHLColor CGColor] 7 8 @interface ViewController () 9 @property (nonatomic, strong) UITextField *show; 10 @property (nonatomic, strong) UISegmentedControl *directBtn; 11 @end 12 13 @implementation ViewController 14 15 - (void)viewDidLoad { 16 [super viewDidLoad]; 17 [self setUpShowTextField]; 18 [self setUpBtn]; 19 } 20 21 #pragma mark - show 22 - (void)setUpShowTextField { 23 UITextField *show = [[UITextField alloc] init]; 24 show.frame = CGRectMake(30, 360, 270, 30); 25 show.textAlignment = NSTextAlignmentCenter; 26 show.text = @"你是猪吗?"; 27 show.textColor = QHLColor; 28 show.layer.cornerRadius = 5; 29 show.layer.masksToBounds = YES; 30 show.layer.borderWidth = 2.0; 31 show.layer.borderColor = QHLCGColor; 32 self.show = show; 33 [self.view addSubview:show]; 34 } 35 #pragma mark - btn 36 - (void)setUpBtn { 37 UIButton *btn = [[UIButton alloc] init]; 38 btn.layer.borderColor = QHLCGColor; 39 btn.frame = CGRectMake(30, 430, 270, 30); 40 btn.layer.borderWidth = 2.0; 41 btn.layer.cornerRadius = 5; 42 btn.layer.masksToBounds = YES; 43 [btn setTitle:@"点我呀" forState:UIControlStateNormal]; 44 [btn setTitle:@"猪是你" forState:UIControlStateHighlighted]; 45 [btn setTitleColor:QHLColor forState:UIControlStateNormal]; 46 [self.view addSubview:btn]; 47 48 [btn addTarget:self action:@selector(btnDidClick) forControlEvents:UIControlEventTouchUpInside]; 49 } 50 #pragma mark - btn 点击事件 51 - (void)btnDidClick { 52 [self.show shakeWithTimes:20 speed:0.05 range:3 shakeDirection:(self.directBtn.selectedSegmentIndex == 0)?QHLDirectionHorizontal:QHLDirectionVertical]; 53 } 54 @end
在 - (void)viewDidLoad {} 中添加一个textField和button,然后设置相关的属性,并给button添加点击事件
当点击事件触发的时候,textField抖动!!!!
自己试了textField 和button的抖动效果 别的没试~~~
新手渣 如果哪里有些错的地方 求大神指点!!!
原文:http://www.cnblogs.com/qhlbk/p/5093406.html