读书人

UITextField颤动动画

发布时间: 2012-09-16 17:33:16 作者: rapoo

UITextField抖动动画

最近做项目,需求中要求用户名或密码输错时,输入框要抖动(类似Mac登录密码错误的抖动效果)。现将代码分享出来:


@interface UITextField (YHShakeUITextField)

- (void) shake;

@end



#import "YH-TextField.h"

#import <QuartzCore/QuartzCore.h>


@implementation UITextField (YHShakeUITextField)


//self.superView

- (void) shake {

CAKeyframeAnimation *keyAn = [CAKeyframeAnimationanimationWithKeyPath:@"position"];

[keyAnsetDuration:0.5f];

NSArray *array = [[NSArrayalloc] initWithObjects:

[NSValuevalueWithCGPoint:CGPointMake(self.center.x,self.center.y)],

[NSValuevalueWithCGPoint:CGPointMake(self.center.x-5,self.center.y)],

[NSValuevalueWithCGPoint:CGPointMake(self.center.x+5,self.center.y)],

[NSValuevalueWithCGPoint:CGPointMake(self.center.x,self.center.y)],

[NSValuevalueWithCGPoint:CGPointMake(self.center.x-5,self.center.y)],

[NSValuevalueWithCGPoint:CGPointMake(self.center.x+5,self.center.y)],

[NSValuevalueWithCGPoint:CGPointMake(self.center.x,self.center.y)],

[NSValuevalueWithCGPoint:CGPointMake(self.center.x-5,self.center.y)],

[NSValuevalueWithCGPoint:CGPointMake(self.center.x+5,self.center.y)],

[NSValuevalueWithCGPoint:CGPointMake(self.center.x,self.center.y)],

nil];

[keyAnsetValues:array];

[arrayrelease];

NSArray *times = [[NSArrayalloc] initWithObjects:

[NSNumbernumberWithFloat:0.1f],

[NSNumbernumberWithFloat:0.2f],

[NSNumbernumberWithFloat:0.3f],

[NSNumbernumberWithFloat:0.4f],

[NSNumbernumberWithFloat:0.5f],

[NSNumbernumberWithFloat:0.6f],

[NSNumbernumberWithFloat:0.7f],

[NSNumbernumberWithFloat:0.8f],

[NSNumbernumberWithFloat:0.9f],

[NSNumbernumberWithFloat:1.0f],

nil];

[keyAnsetKeyTimes:times];

[timesrelease];

[self.layeraddAnimation:keyAn forKey:@"TextAnim"];

}

@end



在登录的类里调用如下:

-(void)isloginbuttonClick

{

if ([userTextField.textlength] == 0)

{

UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"watch out"

message:@"please input user"

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles:nil,nil];

[alertViewshow];

[alertViewrelease];

[userTextFieldshake];

}

elseif ([passwordTextField.textlength] == 0)

{

UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"watch out"

message:@"please input password"

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles:nil,nil];

[alertViewshow];

[alertViewrelease];

[passwordTextFieldshake];

}

else

{

SecondViewController *secondViewController = [[SecondViewControlleralloc] init];

[self.navigationControllerpushViewController:secondViewController animated:YES];

}

}


读书人网 >移动开发

热点推荐