Search code examples
iphoneioscore-animationcore-graphics

Add subview with UIAlertVIew like animation,with a bouncing effect


How to give a bounce or pop up style animation while adding a subview in iOS? I need something similar to UIAlertView appearance style. I know there are several methods to do the same: but I prefer to use basic [UIView animateWithDuration: ...]; block here, so may I get some help regarding the transform I should use here to get the pop up effect?

Thanks


Solution

  • Animation for PopUp style :

    Swift

        yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {() -> Void in
            yourView.transform = .identity
        }, completion: {(finished: Bool) -> Void in
            // do something once the animation finishes, put it here
        })
    

    Reverse Animation with hiding the same View

        yourView.transform = .identity
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {() -> Void in
            yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        }, completion: {(finished: Bool) -> Void in
            // do something once the animation finishes, put it here
            yourView.isHidden = true
        })
    

    Objective-C

    yourView.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        yourView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished){
        // do something once the animation finishes, put it here
    }];
    

    Reverse Animation with hiding the same View

     yourView.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        yourView.transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished){
        yourView.hidden = YES;
    }];