Add subview with UIAlertVIew like animation,with a bouncing effect - iphone

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

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

I had already done before ... you can get my idea from this code . this may help you. if you have any problem, please let me know.thanks
- (CGAffineTransform)transformForOrientation {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIInterfaceOrientationLandscapeLeft) {
return CGAffineTransformMakeRotation(M_PI*1.5);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
return CGAffineTransformMakeRotation(M_PI/2);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
return CGAffineTransformMakeRotation(-M_PI);
} else {
return CGAffineTransformIdentity;
}
}
-(void)showCustomAlertView{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
//self (this view pop up like alert)
[window addSubview:self];
self.transform = CGAffineTransformScale([self transformForOrientation], 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/1.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce1AnimationStopped)];
self.transform = CGAffineTransformScale([self transformForOrientation], 1.1, 1.1);
[UIView commitAnimations];
}
- (void)bounce1AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce2AnimationStopped)];
self.transform = CGAffineTransformScale([self transformForOrientation], 0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounce2AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
self.transform = [self transformForOrientation];
[UIView commitAnimations];
}

Try this:
self.popUpContainerView.alpha = 0;
[UIView animateWithDuration:0.1 animations:^{self.popUpContainerView.alpha = 1.0;}];
CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale"];
bounceAnimation.values = #[#0.01f, #1.1f, #1.0f];
bounceAnimation.keyTimes = #[#0.0f, #0.5f, #1.0f];
bounceAnimation.duration = 0.2;
[self.popUpContainerView.layer addAnimation:bounceAnimation forKey:#"bounce"];

Try these three methodes..
****vwTemp is my custome alert view****
- (void)showAlertView
{
vwTemp.transform = CGAffineTransformMakeScale( 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/1.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce1AnimationStopped)];
vwTemp.transform = CGAffineTransformMakeScale( 1.1, 1.1);
[UIView commitAnimations];
}
- (void)bounce1AnimationStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce2AnimationStopped)];
vwTemp.transform = CGAffineTransformMakeScale (0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounce2AnimationStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
[UIView setAnimationDelegate:self];
vwTemp.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}

yourView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3/1.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce1AnimationStopped)];
[self.view addSubview:yourView];
yourView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
[UIView commitAnimations];
After bouncing animation two time this bellow two methods are used..
- (void)bounce1AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3/2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce2AnimationStopped)];
yourView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounce2AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3/2];
yourView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}

Use below code working in swift3
func popIn(yourView : UIView){
yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
UIView.animateKeyframes(withDuration: 0.2, delay: 0.0, options: UIViewKeyframeAnimationOptions.calculationModeDiscrete, animations: {
yourView.transform = .identity
}, completion: nil)
}
func popOut(yourView: UIView) {
yourView.transform = .identity
UIView.animateKeyframes(withDuration: 0.2, delay: 0.0, options: UIViewKeyframeAnimationOptions.calculationModeDiscrete, animations: {
yourView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
}, completion:{(_ finish : Bool) in
yourView.removeFromSuperview()}
)
}

Related

How to add multiply animation to the view in objective c?

I want to add multiple animations to the view. I just can add one,... I do not want to concatenate
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kAnimationDuration];
//self.square.bounds = CGRectMake(0, 0, self.square.bounds.size.height, 200);
//self.transform = CGAffineTransformMakeScale(2, 1);
CGAffineTransform scaleTrans1 = CGAffineTransformMakeScale(2, 1);
self.transform = scaleTrans1;
[UIView commitAnimations];
You can use animateWithDuration (in any of its variations) from UIView, as long as the properties you are trying to animate are actually animatable (view UIView / Animations).
For example:
[UIView animateWithDuration:1.0f
animations: ^ {
self.square.bounds = CGRectMake(0, 0, self.square.bounds.size.height, 200);
self.transform = CGAffineTransformMakeScale(2, 1);
}];
Hope it helps!
I did like this and its working
- (void)drawRect:(CGRect)rect
{
[UIView animateWithDuration:4.0f animations:^{
self.transform = CGAffineTransformMakeScale(2, 1);
} completion:^(BOOL finished){
[UIView animateWithDuration:4.0f animations:^{
self.transform = CGAffineTransformMakeScale(0.75, 1);
} completion:^(BOOL finished){
[UIView animateWithDuration:4.0f animations:^{
self.transform = CGAffineTransformMakeScale(2, 1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:4.0f animations:^{
self.transform = CGAffineTransformMakeScale(0.75, 1);
}];
}];
}];
}];
}

Multiple UIView animations hitting

I'm animating two uiviews using CGAffineTransform
For 1st view:
swingView.transform = CGAffineTransformRotate(swingView.transform, M_PI/2);
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse
animations:^{
swingView.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
For second view
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationBeginsFromCurrentState:YES];
CGAffineTransform transform = CGAffineTransformMakeTranslation(touchLocation.x, touchLocation.y);
CGAffineTransform transform1 = CGAffineTransformMakeTranslation(-120, 0);
myview.transform = transform1;
[UIView commitAnimations];
Here two views are hitting when animation starts, what i have to do to not allow hitting between these uiviews.

How to scale UIImageView properly with UIView animateWithDuration?

I'm scaling UIImageView and then reverting it using UIView animateWithDuration with UIViewAnimationOptionAutoreverse option. The problem is the image animation is a little bit jaggy (twitching) at the end of the animation. Here's my code:
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionAutoreverse
animations:^{
myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);}
completion:^(BOOL finished){if (finished){
myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);}}];
I know that I'm missing something but don't know where to start :(
UPDATE1: I'm performing nested 6 animations where each next animation performs after previous one. For that I'm using block animations and performing each next animation in complete block.
UPDATE2: I have tried with UIViewAnimationOptionRepeat option but there's still some flash effect after each scale animation.
UIViewAnimationOptionAutoreverse does nothing without also using the UIViewAnimationOptionRepeat option. Your animation as written will shrink the image to 90%, then snap back to 100% in the completion block. Maybe you can follow this animation with another that scales back to 100% over a quarter second. Something like this:
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^{
myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);}
completion:^(BOOL finished){if (finished){
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^{
myImage.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);}
completion:NULL];}}];
.h file declerd.
NSInteger imgint;
.m file in code and try.
-(void)
{
[NSTimer scheduledTimerWithTimeInterval:(0.85f)target:self selector:#selector(play_btn_animation) userInfo:nil repeats:YES];
}
-(void)play_btn_animation
{
if(imgint == 0)
{
[UIView animateWithDuration:0.8
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
img.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);
}
completion:^(BOOL finished) {
img = 1;
}];
}
else
{
[UIView animateWithDuration:0.8
delay:0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
img.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
}
completion:^(BOOL finished) {
imgint = 0;
}];
}
}

How do you rotate UIView, UIImageView or CALayer animated 360˚?

How do you rotate UIView, UIImageView or CALayer animated 360 degress without using OpenGL ES?
#import <QuartzCore/QuartzCore.h>
CABasicAnimation *fullRotation;
fullRotation = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
fullRotation.fromValue = #(0.0);
fullRotation.toValue = #((360.0 * M_PI)/180.0);
fullRotation.duration = 3.5f;
fullRotation.repeatCount = MAXFLOAT;
[view.layer addAnimation:fullRotation forKey:#"rotation-animation"];
If you want to change the anchor point it to rotates around then you can do
CGRect frame = view.layer.frame;
self.anchorPoint = CGPointMake(0.5, 0.5); // rotates around center
self.frame = frame;
Presumably you mean animate rotating a UIImage around a full 360°? In my experience, the way to accomplish a full circle rotation is to chain multiple animations together:
- (IBAction)rotate:(id)sender
{
[UIView beginAnimations:#"step1" context:NULL]; {
[UIView setAnimationDuration:1.0];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
[imageView.transform = CGAffineTransformMakeRotation(120 * M_PI / 180);
} [UIView commitAnimations];
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([animationID isEqualToString:#"step1"]) {
[UIView beginAnimations:#"step2" context:NULL]; {
[UIView setAnimationDuration:1.0];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
imageView.transform = CGAffineTransformMakeRotation(240 * M_PI / 180);
} [UIView commitAnimations];
}
else if ([animationID isEqualToString:#"step2"]) {
[UIView beginAnimations:#"step3" context:NULL]; {
[UIView setAnimationDuration:1.0];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
imageView.transform = CGAffineTransformMakeRotation(0);
} [UIView commitAnimations];
}
}
I've left the easeIn/easeOut curve (instead of linear) in place, so you can see the individual animations.

UIView animation didEndSelector: method not getting called?

I have a UIView that is moved into view. After 2 seconds, I'd like to move the UIView out of view again. How do I do this? I have tried the below, but it doesn't work :(
I have setup the NSLog, but it doesn't look like it's finishing and then calling the loadingViewDisappear: method :(
Thank you.
...
[UIView beginAnimations:#"AnimateIn" context:nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 0.7f];
[UIView setAnimationDidStopSelector:#selector(loadingViewDisappear:finished:context:)];
loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
[UIView commitAnimations];
}
- (void)loadingViewDisappear:(NSString *)animationID finished:(NSNumber *) finished context:(void *) context {
NSLog( (finished ? #"YES!" : #"NO!" ) );
if ([animationID isEqualToString:#"AnimateIn"] && finished) {
[UIView beginAnimations:#"AnimateOut" context:nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseIn];
[UIView setAnimationDelay:2.0f];
[UIView setAnimationDuration: 0.7f];
loadingView.frame = CGRectMake(0, 480, 320, 80);
[UIView commitAnimations];
[loadingView removeFromSuperview];
}
}
You need to set the animation delegate:
[UIView beginAnimations:#"AnimateIn" context:nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration: 0.7f];
//Add this
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(loadingViewDisappear:finished:context:)];
loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
[UIView commitAnimations];