这两天在网上看到一个帖子讨论关于有些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的抖动效果 别的没试~~~
如果哪里有些错的地方 求大神指点!!!