我可以将相同的动画应用于多个文本字段吗?

Can I apply the same animation to multiple text fields?

我正在创建一个方法来摇动我的 UITextField 并使用 POP 来完成繁重的工作。

- (void)textFieldErrorAnimation {

    POPSpringAnimation *shake = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];

    shake.springBounciness = 10;
    shake.velocity = @(2000);


    [self.value.layer pop_addAnimation:shake forKey:@"shakeValue"];
    [self.year.layer pop_addAnimation:shake forKey:@"shakeYear"];
}

当我将动画应用到对象 "value" 时似乎有效,但当它同时应用到 "value" 和 "year" 时似乎无效。

我知道我可以创建一个 shake2、shake3 等,所以基本上每个 textField 一个,但我需要这样做似乎很奇怪。

我有什么想法可以重用这个动画吗?

当然可以。我真正要做的是,这种重用动画是创建一种 returns 动画并正确设置名称和键值的方法。

- (POPSpringAnimation *)popShakeAnimation {


POPSpringAnimation *shakeAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
shakeAnimation.velocity = @2000;
shakeAnimation.springBounciness = 20;
shakeAnimation.name = @"shakeAnimation";

return shakeAnimation;

}

然后每当我需要摇动任何视图时,我只需将它们添加到视图层即可。

    [self.textfield001.layer pop_addAnimation:[self popShakeAnimation]
                                            forKey:@"shakeAnimation"];
    [self.textfield002.layer pop_addAnimation:[self popShakeAnimation]
                                            forKey:@"shakeAnimation"];
    [self.textfield003.layer pop_addAnimation:[self popShakeAnimation]
                                              forKey:@"shakeAnimation"];

请记住首先将键值设置为您使用动画创建的名称。