UILabel和UITextField的动画(Facebook POP框架)



当用户输入错误时,我正在使用Facebook POP框架为UILabelsUITextFields设置动画。我想模拟一下当你在mac上输入错误密码时的春季动画。

我写了这个方法来做抖动动画:

- (void)shakeAnimation:(UITextField *)textField :(UILabel *)label
{
  //This method takes a textField and a label as input, and animates them accordingly. 
  //When the animation is done, the button is available for touch again, and the animation is removed.
  POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
  positionAnimation.springBounciness = 20; //just parameters, nothing fancy
  positionAnimation.velocity = @4000;
  [positionAnimation setCompletionBlock:^(POPAnimation *animation, BOOL finished) {
    self.signUpButton.userInteractionEnabled = YES;
  }];
  [textField.layer pop_addAnimation:positionAnimation forKey:@"positionAnimation"];
  [label.layer pop_addAnimation:positionAnimation forKey:@"positionAnimation"];
  [positionAnimation removedOnCompletion];
}

然而,当我这样调用这个方法时:[self shakeAnimation:self.emailField :self.emailLabel];

UILabels只向右移动大约50px,然后停止移动,而UITextFields什么都不做。它看起来一点也不像动画,只是对帧的调整。

不要将相同的POPSpringAnimation同时添加到textField层和label层,而是制作两个POPSpringAnimation并分别添加一个。像这样:

POPSpringAnimation *anim1 = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
anim1.springBounciness = 20; //just parameters, nothing fancy
anim1.velocity = @400;
[anim1 setCompletionBlock:^(POPAnimation *animation, BOOL finished) {
    self.button.userInteractionEnabled = YES;
}];
POPSpringAnimation *anim2 = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
anim2.springBounciness = 20; //just parameters, nothing fancy
anim2.velocity = @400;
[textField.layer pop_addAnimation:anim1 forKey:@"positionAnimation"];
[label.layer pop_addAnimation:anim2 forKey:@"positionAnimation"];
[anim1 removedOnCompletion];
[anim2 removedOnCompletion];

最新更新