Search code examples
iosanimationcore-animationquartz-graphics

How to replace one UI element by another with custom animation?


I want to implement nested commentaries(like stickers) in my own document viewer. At first, it should be UITextView, but when resignFirstResponder executes, it should become just a small button. The main question is: how to animate this? I've read Quartz 2d programming guide from Apple, but it didn't gave me any ideas. I don't asking for exact or ready solution: keywords, links to articles or documentation are enough. Thanks for future responses.


Solution

  • You could use this method

    [UIView animateWithDuration: delay: options: animations: completion:];
    

    So if you wanted to fade in a button and fade out the textfield it would be

    //Starting properties
    myButton.alpha = 0;
    myTextField.alpha = 1;
    
    //Do the animation
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{
        myButton.alpha = 1;
        myTextField.alpha = 0;   
    } completion:^(BOOL finished) {
        if (finished) {
            NSLog(@"finished animating");
        }
    }];
    

    This will change the opacity of the 2 objects from 0 - 1 / 1 - 0 over 300ms

    You can animate many properties this way like size, position, opacity etc.