这两天在网上看到一个帖子讨论关于有些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 [s