iOS 编辑框抖动提示

参考http://blog.csdn.net/leeyehong_self/article/details/7943042 自己做的demo,大部分代码来自他的,我只是修改一些我工程里报错的地方。


1.创建一个类的头文件和源文件

#import <Foundation/Foundation.h>

@interface UITextField (YHShakeUITextField)

- (void) shake;

@end
#import "YH-TextField.h"
#import <QuartzCore/QuartzCore.h>

@implementation UITextField (YHShakeUITextField)

//self.superView

- (void) shake {
    
    CAKeyframeAnimation *keyAn = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    [keyAn setDuration:0.5f];
    
    NSArray *array = [[NSArray alloc] initWithObjects:
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)],
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x-5, self.center.y)],
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x+5, self.center.y)],
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)],
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x-5, self.center.y)],
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x+5, self.center.y)],
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)],
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x-5, self.center.y)],
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x+5, self.center.y)],
                      
                      [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)],
                      
                      nil];
    
    [keyAn setValues:array];
    
    [array release];
    
    NSArray *times = [[NSArray alloc] initWithObjects:
                      
                      [NSNumber numberWithFloat:0.1f],
                      
                      [NSNumber numberWithFloat:0.2f],
                      
                      [NSNumber numberWithFloat:0.3f],
                      
                      [NSNumber numberWithFloat:0.4f],
                      
                      [NSNumber numberWithFloat:0.5f],
                      
                      [NSNumber numberWithFloat:0.6f],
                      
                      [NSNumber numberWithFloat:0.7f],
                      
                      [NSNumber numberWithFloat:0.8f],
                      
                      [NSNumber numberWithFloat:0.9f],
                      
                      [NSNumber numberWithFloat:1.0f],
                      
                      nil];
    
    [keyAn setKeyTimes:times];
    
    [times release];
    
    [self.layer addAnimation:keyAn forKey:@"TextAnim"];
    
}

@end
2.导入系统的QuartzCore库

3.把创建的类加入compile Sources 并且去掉ARC

4.在事件里引用

- (IBAction)selectLoginAction:(id)sender {
    
    if ([email.text length] == 0)
    { 
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"watch out"
                                  
                                                            message:@"please input user"
                                  
                                                           delegate:nil
                                  
                                                  cancelButtonTitle:@"OK"
                                  
                                                  otherButtonTitles:nil,nil];
        [alertView show];    
        [email shake];
    }
    else if ([password.text length] == 0)
        
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"watch out"
                                  
                                                            message:@"please input password"
                                  
                                                           delegate:nil
                                  
                                                  cancelButtonTitle:@"OK"
                                  
                                                  otherButtonTitles:nil, nil];
        [alertView show];
        [password shake];
    }else 
    {     
        LoginViewController *loginViewController = [[LoginViewController alloc] init];
        
        [self.navigationController pushViewController:loginViewController animated:YES]; 
    }
}



你可能感兴趣的:(iOS 编辑框抖动提示)