做项目遇到一个需求,就是再输入手机号时如果手机号输入错误,textField要颤抖一下用来警示用户输入错误;网上也找过一些,那些都是封装好的,今天自己找到苹果自带的CAlayer动画可以实现,自己了一个方法;用苹果自带的API实现;
首先导入QuartzCore.framework这个框架,然后在控制器页面导入#import
#点击来确定输入内容是否正确
- (void)isValidate
{
if(![textField.textisEqualToString:@"code4app"]) {
[selfshakeAnimationForView:textField];
}
}
-(void)shakeAnimationForView:(UIView*)view
{
CALayer*lay_lb = [viewlayer];
CGPointpos_lb = [lay_lbposition];
CGPointy =CGPointMake(pos_lb.x-10, pos_lb.y);
CGPointx =CGPointMake(pos_lb.x+10, pos_lb.y);
CABasicAnimation* animation = [CABasicAnimationanimationWithKeyPath:@"position"];
[animationsetTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animationsetFromValue:[NSValuevalueWithCGPoint:x]];
[animationsetToValue:[NSValuevalueWithCGPoint:y]];
[animationsetAutoreverses:YES];
[animationsetDuration:0.08];
[animationsetRepeatCount:3];
[lay_lbaddAnimation:animationforKey:nil];
}
网友评论