UITextField границы анимации - PullRequest
       11

UITextField границы анимации

3 голосов
/ 07 марта 2012

Я пытаюсь анимировать цвет границы UITextField (постепенно).Вот что я пытаюсь

tmp.layer.borderColor= [UIColor colorWithRed:255.0 green:0.0 blue:0.0 alpha:0.0].CGColor;
tmp.layer.borderWidth = 1.0f;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
tmp.layer.borderColor= [UIColor colorWithRed:255.0 green:0.0 blue:0.0 alpha:1.0].CGColor;
[UIView commitAnimations];

Приведенный выше код добавляет красную рамку к textFields, но без анимации.

Ответы [ 2 ]

2 голосов
/ 28 августа 2015

У меня возникли проблемы с анимацией границ UITextField, поэтому я хотел бы предоставить свое решение. Я смог выполнить свою задачу, используя CABasicAnimation для выполнения анимации UITextField.

Ниже приведен блок кода:

textField.layer.borderWidth = 1.0f;

[CATransaction begin]; {
    [CATransaction setCompletionBlock:^{
        // How the textField should look at the end of the animation.
        textField.layer.borderColor = [UIColor colorWithRed:255.0 green:0.0 blue:0.0 alpha:1.0].CGColor;

    }];

    // The keyPath specifies which attribute to modify. In this case it's the layer's borderColor.
    CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"borderColor"];

    // fromValue provides the beginning state of the animation.
    colorAnimation.fromValue = (__bridge id)[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0].CGColor;

    // The to value is the final state of the animation. NOTE: That upon completion of the animation is removed.
    colorAnimation.toValue   = (__bridge id)[UIColor colorWithRed:255.0 green:0.0 blue:0.0 alpha:1.0].CGColor ;
    color.duration = 2.0;

    // Finally you add the animation to the textField's layer.
    [textField.layer addAnimation:colorAnimation forKey:@"color"];

} [CATransaction commit];

Теперь анимация - это просто слой, наложенный временно поверх существующего. Таким образом, чтобы сделать конечный результат анимации постоянным, есть два метода:

  1. Вы можете установить конечный результат для слоя UITextField в блоке завершения CATransaction. Как видно из приведенного выше кода.

  2. Путем установки получателя анимации как видимого в его конечном состоянии и предотвращения удаления анимации. Как видно ниже:

        // Prevents the removal of the final animation state from visibility. 
        // Add these before you add the animation to the textField's layer.
        colorAnimation.fillMode = kCAFillModeForwards;
        colorAnimation.removedOnCompletion = false;
    
0 голосов
/ 07 марта 2012

Эти свойства не подлежат анимации.Юй должен будет удалить границы вашего текстового поля и создать какой-то другой UIView с границами, которые вы хотите, позади вашего текстового поля.Также установите его альфа-свойство на 0.0 и анимируйте его на 1.0, когда вам нужно.(альфа-свойство является анимируемым, поэтому оно будет работать за вас).

EDIT.

tmp.layer.borderColor= [UIColor redColor].CGColor;
tmp.layer.borderWidth = 1.0f;
tmp.alpha = 0.0;
//somewhere here should be the [someView addSubview:tmp];
//and after that should go the [someView addSubview:yourTextFieldView];

//somewhere later in your code, when you need the animation to happen
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
tmp.alpha = 1.0;
[UIView commitAnimations];

Также вы можете использовать так называемую одностадийную анимацию вот так:

[UIView animateWithDuration:2.0 animations:^{
    tmp.alpha = 1.0;
}];
...