Card flipping animation - ios

I am making a card game and would like to use a Card flipping animation.
Currently I'm using this code with two images, front and back:
card, is a uiview with two UIImageView propertys
[UIView transitionWithView:card duration:0.65f
options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
card.backImageView.image = card.frontImageView.image;
card.layer.shadowOpacity = 0.8;
} completion:^(BOOL finished){
card.layer.shadowOpacity = 0.0;
}];

In order to create a basic card flipping animation like the one in the video you've linked to, I suggest putting frontImageView and the backImageView directly on top of each other on the UIView you intend to flip. To start, set their images to front and back accordingly; and, in this particular case, hide the frontImageView and show the backImageView.
Assuming "card" is the UIView you intend to flip, to perform the flip try:
[UIView transitionWithView:card duration:0.65f
options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
frontImageView.hidden = NO;
backImageView.hidden = YES;
} completion:^(BOOL finished) {
// whatever you'd like to do immediately after the flip completes
}];
Edit:
And to handle the shadow, first off, it appears in the video you posted that the shadow grows in length moreso than it just fades in. And it seems as if (and makes logical sense that) the shadow reaches its peak during the middle of the animation as the card is lifted at its highest point. Since the shadow grows then shrinks during the course of the flip animation, it doesn't make sense to include the shadow animation within the same animation block as the flip since they're on different time schedules.
Secondly with regard to the shadow, to animate the layer property, you have to use Core Animations.
Perhaps you can run the two animations concurrently, i.e. while the above animation is performing, also do something like:
CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:#"shadowRadius"];
shadowAnimation.delegate = self;
[shadowAnimation setFromValue:[NSNumber numberWithFloat:3.0]];
[shadowAnimation setToValue:[NSNumber numberWithFloat:10.0]];
[shadowAnimation setDuration:0.65f];
shadowAnimation.autoreverses = YES;
[[card layer] addAnimation:shadowAnimation forKey:#"shadowRadius"];
The last portion has been adapted from this code and takes advantage of the autoreverse property to automatically reverse the shadow's growth.

This is how to perform a card flipping animation in swift 3:
UIView.transition(from: frontImageView,
to: backImageView,
duration: 0.65,
options: .transitionFlipFromRight,
completion: nil)

i tested a few things, and Compromise with this:
_tempTransform = card.transform;
[card loadFront];//loads the front image.
card.frontImageView.hidden=YES;
[UIView animateWithDuration:0.02f
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0 / -500;
transform = CATransform3DRotate(transform, 15.0f * M_PI / 180.0f, 0, -1, 0.0f);
card.layer.transform = transform;
}completion:^(BOOL done){
card.layer.shadowOpacity=0.1f;
card.transform=CGAffineTransformScale(card.transform, 1.2f, 1.2f);
[UIView transitionWithView:card duration:0.4f
options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
card.frontImageView.hidden=NO;
card.backImageView.hidden=YES;
} completion:^(BOOL finished){
card.layer.shadowOpacity=0.0f;
card.transform=_tempTransform;
}];
}];

Related

UIImageview rotation animation

I want to apply an animation to two UIImageViews. The idea is for image one to flip horizontally by 90 degrees and then image two to complete the rotation. Think of it as a coin spinning: Head side (image one) faces forward -> turn 90 degrees -> Tail side (image two) rotates to face forward. I can do the first half of the animation but I am stuck on the second.
[UIView animateWithDuration:1.0f animations:^{
image1.transform = CGAffineTransformMakeScale(-0.01, 1);
} completion: ^(BOOL finished) {
// How can I make image 2 to rotate out as if it initially was already rotated by 90 degrees?
}];
for flipping animation, there a animation option called UIViewAnimationOptionTransitionFlipFromRight, use it with the UIView's animation method for example, like below for example
[UIView transitionWithView:myImageView //with first image
duration:animationDuration
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
myImageView.image = toImage; //to next image
} completion:^(BOOL finished) {
//completion actions after flipped
}];
there are other animation options also there, like FromLeft,
FromTop,FromBottom use any one to your requirement
Take a look at this question about flipping a UIView in 3D using CATransform3D.
Simple Core Animations 3D transform of UIView
The answer:
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0 / -1000.0;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, M_PI * 0.6, 1.0f, 0.0f, 0.0f);
[UIView animateWithDuration:1.0 animations:^{
self.someView.layer.anchorPoint = CGPointMake(0.5, 0);
self.someView.layer.transform = rotationAndPerspectiveTransform;
} completion:^(BOOL finished){
// code to be executed when flip is completed
}];

How to reduce the opacity of an UIButton gradually?

In my project i have an UIButton which on clicking zooms. Everything is fine till here but i require the opacity of the button to gradually decrease from 1 to 0. I mean it should decrease in 1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0 sequence. My current code is
-(void)roundButtonAnimation
{
self.buttonZoom = [CAKeyframeAnimation animationWithKeyPath:#"transform"];
self.buttonZoom.duration = 0.8f;
self.buttonZoom.values = #[[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(2,2,2)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(3,3,3)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(5,5,5)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(6,6,6)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(7,7,7)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(9,9,9)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(10,10,10)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(11,11,11)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(12,12,12)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(13,13,13)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(14,14,14)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(15,15,15)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(16,16,16)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(17,17,17)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(18,18,18)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(50,50,50)]];
self.buttonZoom.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
self.roundButton.transform = CGAffineTransformMakeScale(0,0);
self.roundButton.alpha = 0.3;
[self.roundButton.layer addAnimation:self.buttonZoom forKey:#"buttonScale"];
[self.CLSpinnerView1 stopAnimating];
//[self performSelector:#selector(secondView) withObject:self afterDelay:0.3];
[self performSelector:#selector(postNotification) withObject:self afterDelay:0.6];
}
[UIView animateWithDuration:1.0
delay:0.0
options: UIViewAnimationCurveLinear
animations:^{
self.roundButton.transform = CGAffineTransformMakeScale(50.0f, 50.0f);
self.roundButton.alpha = 0.0;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
}
Let's explain a little, in this animation block you say that the animation should take place in one second, during this second based on a linear curve (thus always equal increment/decrement) the button should zoom 50 times and alpha should be animate to 0.
I do not understand why you are using a more complicated CAKeyframeAnimation over that simple animation block.
Keyframe animation are useful when you want more power over timings and animate over a set of values.
UIView animateWithDuration:animations: should serve your purpose here.
[UIView animateWithDuration:1.0 animations:^{
button.alpha = 0.0;
}];
Will gradually change the button's alpha to 0 over one second (...WithDuration:1.0...).
See the The UIView Class Reference for more information.
Hope this helps.
Try this
[button.layer removeAnimationForKey:#"opacity"];

How to move uiview on this trajectory

Can me explane how I can rotate image use anchor point like
https://www.dropbox.com/s/vh3h5cr1mkdbfh3/ex_image2.JPG
on .m
#import <QuartzCore/QuartzCore.h>
on .h
[UIView animateWithDuration:0.7f
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^
{
self.center = position;
self.layer.anchorPoint = CGPointMake(-1, 0);
self.transform = CGAffineTransformMakeRotation(-5);
}
completion:^(BOOL completed){
}];
When I use this code I have something like that
https://www.dropbox.com/s/v87abux9dqm4y0p/ex_image1.JPG
When you apply a rotation transform to a layer, the rotation occurs around the anchor point.
So set the layer's anchor point to (0.0, 0.0) first.
self.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
Then you can just rotate the view and the rotation will occur around the anchor point, (0.0, 0.0).
[UIView animateWithDuration:2.0f animations:^{
self.transform = CGAffineTransformMakeRotation(3.1415f);
}];
The code will rotate the view one whole time in the way you have pictured.
You can check out the section titled "Anchor Points Affect Geometric Manipulations" in Apple's "Core Animation Programming Guide" for more information.

UIView group animation

I'm trying to implement the following animation:
yourSubView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//change time duration according to requirement
// animate it to the identity transform (100% scale)
yourSubView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// if you want to do something once the animation finishes, put it here
}];
combined with some movement of the subview. I know in core animation you can combine animations but how can you do that in UIView animation?
Can I translate this code from UIView animation to Core animation?
yourSubView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//change time duration according to requirement
// animate it to the identity transform (100% scale)
yourSubView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// if you want to do something once the animation finishes, put it here
}];
You can combine animations by changing more than one animatable property. For example, you can also set yourSubView.alpha, then change the alpha within the animation block. It will combine the scale and alpha change together.
To do a simple translation, try this in your animation block:
yourSubView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 100.0, 100.0);
this should set the scale back to identity along with moving 100px in x and y direction.
For Core Animation, this Grouping two Core Animations with CAAnimationGroup causes one CABasicAnimation to not run should get you started. You'll use a CAAnimationGroup to combine multiple animations, and can do pretty fancy stuff including 3D rotation.
Scaling:
CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale"];
[scale setValues:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.01f],[NSNumber numberWithFloat:1.0f],nil]];
[scale setKeyTimes:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.4f],nil]];
[mySubview.layer addAnimation:scale forKey:#"myScale"];

ios CATransform3DMakeScale scaleup animation

I have the following animation:
CATransform3D scaleTransform = CATransform3DMakeScale(0.1,0.1, 1.0);
scaleAnimation.toValue = [NSValue valueWithCATransform3D:scaleTransform];
keyframeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];;
where I'm scaling down a subview. But I'm trying to do opposite. start from .01% of the size of the subview and go to 100% of the size of the subview. Does anyone knows how can I do this?
Do this:
yourSubView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//change time duration according to requirement
// animate it to the identity transform (100% scale)
yourSubView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// if you want to do something once the animation finishes, put it here
}];

Resources