IOS中對於一些控制項的抖動效果,IOS控制項抖動效果

來源:互聯網
上載者:User

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的抖動效果 別的沒試~~~ 

新手渣 如果哪裡有些錯的地方 求大神指點!!!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.