#import <UIKit/UIKit.h>
@interface UITextField (Shake)
- (void)shake;
@end
#import "UITextField+Shake.h"
/**
* 为textField扩展一个左右晃动的动画
*/
@implementation UITextField (Shake)
- (void)shake {
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
keyFrame.duration = 0.3;
CGFloat x = self.layer.position.x;
keyFrame.values = @[@(x + 30), @(x - 30), @(x + 20), @(x - 20), @(x + 10), @(x - 10), @(x + 5), @(x - 5)];
[self.layer addAnimation:keyFrame forKey:@"shake"];
}
@end
使用的时候只需要用 UITextField 对象调用 shake方法就可以实现效果了,很简单。谢谢SDC201