IOS中對於一些控制項的抖動效果,IOS控制項抖動效果
這兩天在網上看到一個文章討論關於有些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 @end13 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 - 155 speed:speed56 range:range57 shakeDirection:shakeDirection58 currentTimes:currentTimes + 159 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 @end12 13 @implementation ViewController14 15 - (void)viewDidLoad {16 [super viewDidLoad];17 [self setUpShowTextField];18 [self setUpBtn];19 }20 21 #pragma mark - show22 - (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 - btn36 - (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的抖動效果 別的沒試~~~
新手渣 如果哪裡有些錯的地方 求大神指點!!!