Graphical animation UITextField

I am trying to animate the border color of a UITextField (fades out). This is what I am trying

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];

      

The above code adds a red border to the text boxes, but no animation.

+3


source to share


2 answers


These properties are not animated. Yu'll need to remove the textbox borders and create another UIView with the borders you want in your textbox. Also set its alpha value to 0.0 and animate it to 1.0 when you need it. (the alpha property is animated, so this will do the job for you).

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];

      



Alternatively, you can use a so-called one - step animation like this:

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

      

0


source


I am having trouble animating the borders of a UITextField, so I would like to provide my solution. I was able to accomplish my task using the CABasicAnimation to animate the UITextField.

Below is the code:

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 the layer 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 layer.
    [textField.layer addAnimation:colorAnimation forKey:@"color"];

} [CATransaction commit];

      



Now the animation is just a layer temporarily overlaid on top of the existing one. Therefore, there are two methods to make the end result of the animation permanent:

  • You can set the end result to the UITextField level in the CATransaction completion block. As you can see from the above code.

  • By setting the animation receiver as a visible end state in it and preventing the animation from being removed. As seen below:

        // Prevents the removal of the final animation state from visibility. 
        // Add these before you add the animation to the textField layer.
        colorAnimation.fillMode = kCAFillModeForwards;
        colorAnimation.removedOnCompletion = false;
    
          

+2


source







All Articles