UIView: rotation + movement - iphone

I'm trying to move and rotate a UIImageView. I'm not using Core Animation for this nor I can use it.
By the way, the core part of the animation is what follows:
CGRect r = self.frame;
r.origin.y -= self.gravity + spring;
r.origin.x -= 1;
self.rotation -= 0.015;
self.transform = CGAffineTransformMakeRotation(rotation);
self.frame = r;
It almost works. I does move or it does rotate in place, but when I try to move and rotate it, it completely deforms skewing.
I've tried to move and then rotate and viceversa. I also tried various values for content mode with no luck. Any help?
EDIT
useful tutorial: http://www.permadi.com/blog/2009/05/iphone-sdk-bouncing-and-rotating-ball-example/

It's hard to be sure what's going on without a picture of what you are seeing, but the following may be useful:
When you apply a rotation to a view it rotates it around (0,0), so you need to make sure you set the bounds of your view so that (0,0) is where you want the center of rotation. Suppose the image you are rotating is a 100x200 image and you want to rotate around the center of the image then you would need to say self.bounds = CGMakeRect(-50, -100, 100, 200); If you don't do this it will rotate about it's upper-left corner making sort of a spiral.
Oh, and using self.center to set the view's position seems to be more predictable than using self.frame when you have applied a rotation.
Here's some demo code that might make it easier to understand. In this case the code that does the animation is the view controller, but you should get the idea:
#define USE_SELF_CENTER 0
- (void)viewDidLoad {
[super viewDidLoad];
self.rotation = 0.0f;
self.translation = CGPointMake(self.view.bounds.size.width/2.0f, 0.0);
ObjectView* ov = [[ObjectView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
#if USE_SELF_CENTER==1
ov.center = self.translation;
#else
ov.transform = CGAffineTransformMakeTranslation(self.translation.x, self.translation.y);
#endif
// adjust bounds for center of rotation
ov.bounds = CGRectMake(-50.0f, -50.0f, 100.0f, 100.0f);
[self.view addSubview:ov];
self.objectView = ov;
[ov release];
NSTimer *aTimer = [NSTimer timerWithTimeInterval:0.5 target:self selector:#selector(nextFrame)
userInfo:nil repeats:YES];
self.timer = aTimer;
[aTimer release];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)nextFrame
{
NSLog(#"nextFrame");
self.rotation += (5.0 / 180.0f * M_PI);
self.translation = CGPointMake(self.translation.x, self.translation.y + 2.0);
#if USE_SELF_CENTER==1
CGAffineTransform t = CGAffineTransformMakeRotation(self.rotation);
self.objectView.center = self.translation;
self.objectView.transform = t;
#else
CGAffineTransform rot = CGAffineTransformMakeRotation(self.rotation);
CGAffineTransform txy = CGAffineTransformMakeTranslation(self.translation.x, self.translation.y);
CGAffineTransform t = CGAffineTransformConcat(rot, txy);
self.objectView.transform = t;
#endif
}
The code demonstrates two ways of doing what you want. If USE_SELF_CENTER is zero then all the animation is done via transforms. If USE_SELF_CENTER is non-zero a transform is used for rotation and the translation is accomplished by setting the center of the view.

use something like this, the idea is combine both the transformation i.e.
CGAffineTransform transforms = CGAffineTransformConcat(CGAffineTransformMakeTranslation(-1, -(self.gravity + spring)),CGAffineTransformMakeRotation(rotation));
self.transform = transforms;
Hope this will work

Hello Friend please use this steps. On your XIB, take UIImageView and set Image and set its IBOutlet.
In .h file
int theta;
NSTimer *tmrAnimation;
IBOutlet UIImageView *imgViewLoader;
In .m file
- (void)viewDidLoad
{
[super viewDidLoad];
theta = 0;
tmrAnimation = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(animationImageViewLoader) userInfo:nil repeats:YES];
}
Add This function in your .m file.
-(void)animationImageViewLoader
{
CGFloat angle = theta * (PI / 100);
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
theta = (theta + 1) % 200;
imgViewLoader.transform = transform;
}
This code will help for Image Rotation only....

Following code helped me in achieving translation(vertical movement) and rotation together.
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -200);
transform = CGAffineTransformRotate(transform, M_PI);
[UIView transitionWithView:self
duration:0.5
options:UIViewAnimationOptionAllowAnimatedContent<br>
animations:^{
[view setTransform:transform];
}
completion:^(BOOL finished){
}];
Hope this will help someone else...

Related

animating UIView transitions like expand dot to circle

In my iPhone app i need to implement a different type of transition.
that was
next view open from the current view,
it stats like a dot, and the dot expands slowly like a circle with in the circle next view is to be displayed partially with in the part of the circle , finally the circle expands totally the next view appears completely.
I search lots of transitions like CATransitions, and some animations on cocoa controller, but i didn't find this type of transition, can any one help me please.
Well I think I can offer you a workaround. Instead of pushing to the next view like a dot . I suggest you add a simple dot animation in the ViewWillAppear of the view in which you have to get pushed. Now the push method would remain the same like
[self.navigationController pushViewController:NewView animated:YES];
But in the ViewWillAppear the code would be such that the dot would expand to a circle and reveal the New View beneath it. Hope you understand the logic I am trying to explain here. Any issue do let me know .
in my case I did it that way:
set a CAShapeLayer instance as the layer's mask property of your custom view subclass
#interface MyCustomView ()
#property (nonatomic, strong) CircleShapeLayer *circleShapeLayer;
#end
#implementation MyCustomView
- (id) initWithFrame: (CGRect) frame {
self = [super initWithFrame: CGRectZero];
if (self) {
self.layer.mask = self.shapeLayer;
[self.layer.mask setValue: #(0) forKeyPath: #"transform.scale"];
}
return self;
}
zoom this mask layer to fullsize. code of your view:
- (void) zoom {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: #"transform.scale"];
animation.fromValue = [self.layer.mask valueForKeyPath: #"transform.scale"];
animation.toValue = #(1);
animation.duration = 2.0;
animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
animation.delegate = self;
// Important: change the actual layer property before installing the animation.
[self.layer.mask setValue: animation.toValue forKeyPath: animation.keyPath];
// Now install the explicit animation, overriding the implicit animation.
[self.layer.mask addAnimation: animation forKey: animation.keyPath];
return;
}
- (CAShapeLayer *) circleShapeLayer {
if (!_ circleShapeLayer) {
_circleShapeLayer = [SGMaskLayer layer];
_circleShapeLayer.delegate = _shapeLayer;
_circleShapeLayer.frame = self.bounds;
_circleShapeLayer.needsDisplayOnBoundsChange = YES;
}
return _circleShapeLayer;
}
#end
the code of the mask layer:
#interface CircleShapeLayer : CAShapeLayer
#end
#implementation CircleShapeLayer
- (void) drawLayer: (CALayer *) layer inContext: (CGContextRef) ctx {
UIGraphicsPushContext(ctx);
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect: self.bounds];
self.path = circlePath.CGPath;
UIGraphicsPopContext();
}
#end
from the documentation:
The layer’s alpha channel determines how much of the layer’s content
and background shows through. Fully or partially opaque pixels allow
the underlying content to show through but fully transparent pixels
block that content.
The default value of this property is nil nil. When configuring a
mask, remember to set the size and position of the mask layer to
ensure it is aligned properly with the layer it masks.
so I drew a circle with UIBezierPath to achieve the round mask. at the beginning I set the mask's scale factor to 0 so nothing of the view's layer is visible. then the scale factor is set to 1 (filling the layer's bounds) animated which gives a nice animation of a circle increasing it's radius from the center.
you might need one more animation shifting the center point of your view. both animations can be wrapped in a CAAnimationGroup.
Open in first view:
//Delegate method for annotation did select
- (void)tapOnAnnotation:(RMAnnotation *)annotation onMap:(RMMapView *)map;
{
//To get the touch point of the GMS marker to set them as circle transiton pos
CGPoint markerPoint = annotation.position;
x = markerPoint.x;
y = markerPoint.y;
circleSize = 10;
radiusChange = 0;
//Populate Same Values to next view to close
VenueScreen.x = x;
VenueScreen.y = y;
VenueScreen.view.hidden = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(openVenueScreen) userInfo:nil repeats:YES];
VenueScreen.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:VenueScreen.view];
}
//Circular transition to open Next view
-(void)openVenueScreen
{
VenueScreen.view.hidden = NO;
VenueScreen.view.userInteractionEnabled = NO;
VenueScreen.view.alpha = 0.9;
self.view.userInteractionEnabled = NO;
int rChange = 0; // Its doing proper masking while changing this value
int radius = circleSize-rChange;
CAShapeLayer *circleShapeLayer = [CAShapeLayer layer];
// Make a circular shape
circleShapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x+radiusChange, y+radiusChange, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
radiusChange = radiusChange-10;
// Configure the apperence of the circle
[[VenueScreen.view layer] setMask:circleShapeLayer];
//Start Transition
circleSize = circleSize+10;
if (circleSize > 480)
{
[[VenueScreen.view layer] setMask:nil];
[timer invalidate]; //Stop titmer
VenueScreen.view.userInteractionEnabled = YES;
self.view.userInteractionEnabled = YES;
VenueScreen.view.alpha = 1;
//Populate to next view to close
VenueScreen.radiusChange = radiusChange;
}
}
Close in Next View:
//Close button Action
-(IBAction)DismissVenueScreen:(id)sender;
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(dismissVenueScreen) userInfo:nil repeats:YES];
NSLog(#"close button clciked");
}
//Circular trasition to Close window
-(void)dismissVenueScreen
{
int rChange = 0; // Its doing proper masking while changing this value
int radius = circleSize-rChange;
CAShapeLayer *circleShapeLayer = [CAShapeLayer layer];
// Make a circular shape
circleShapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x+radiusChange,y+radiusChange, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Configure the apperence of the circle
[[self.view layer] setMask:circleShapeLayer];
self.view.layer.masksToBounds = YES;
radiusChange = radiusChange+10;
circleSize = circleSize-10;
if (circleSize <= 0)
{
[timer invalidate]; //Stop titmer
[[self.view layer] setMask:nil];
[self.view removeFromSuperview];
circleSize = 480;
}
}

Transforming view coordinates with CGAffineTransform

I'm fooling around with Apple's ScrollViewSuite, TapToZoom app, trying to place a view on the zoomed image. Here's what I want to do: on double tap, rotate the image by pi/2, zoom it by a factor of 8, then place a subview in the scrollView on top of the transformed image.
I know the view's desired frame in the original coordinate system. So I need to transform those coordinates to the new system after the zoom...
From the ScrollViewSuite...
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
// double tap zooms in
float newScale = [imageScrollView zoomScale] * 8; // my custom zoom
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[imageScrollView zoomToRect:zoomRect animated:YES];
[self rotateView:imageScrollView by:M_PI_2]; // then the twist
}
My rotation code...
- (void)rotateView:(UIView *)anImageView by:(CGFloat)spin {
[UIView animateWithDuration:0.5 delay: 0.0
options: UIViewAnimationOptionAllowUserInteraction
animations:^{
anImageView.transform = CGAffineTransformMakeRotation(spin);
}
completion:^(BOOL finished){}];
}
Then, after the zoom is finished, place a view at a known location in the original coordinate system. This is where I think I need some help...
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)_scale {
CGFloat coX = scrollView.contentOffset.x;
CGFloat coY = scrollView.contentOffset.y;
// try to build the forward transformation that got us here, then invert it
CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI_2);
CGAffineTransform scale = CGAffineTransformMakeScale(_scale, _scale);
// should these be negative? some confusion here.
CGAffineTransform translate = CGAffineTransformMakeTranslation(coX, coY);
// what's the right order of concatenation on these? more confusion
CGAffineTransform aft0 = CGAffineTransformConcat(rotate, scale);
CGAffineTransform aft1 = CGAffineTransformConcat(aft0, translate);
// invert it
CGAffineTransform aft = CGAffineTransformInvert(aft1);
// this is the fixed rect in original coordinate system of the image
// the image is at 0,0 320, 300 in the view, so this will only work
// when the image is at the origin... need to fix that later,too.
CGRect rect = CGRectMake(248, 40, 90, 160);
CGRect xformRect = CGRectApplyAffineTransform(rect, aft);
// build a subview with this rect
UIView *newView = [scrollView viewWithTag:888];
if (!newView) {
newView = [[UIView alloc] initWithFrame:xformRect];
newView.backgroundColor = [UIColor yellowColor];
newView.tag = 888;
[scrollView addSubview:newView];
} else {
newView.frame = xformRect;
}
// see where it landed...
NSLog(#"%f,%f %f,%f", xformRect.origin.x, xformRect.origin.y, xformRect.size.width, xformRect.size.height);
}

How To Rotate an UIImageView based on a point in Touches Moved Method?

Dear All,
I want to rotate a UIImageView relative to a point in touchesMoved method.
I tried 2 -3 methods but I am not getting the exact result what I expected to be.
First method I used
CGAffineTransform transforms = CGAffineTransformMakeRotation(M_PI/2);
imgView.transform = transforms;
This is written in touchesMoved. So I expect a rotation for the image view in each touchesMoved.
But the rotation is occuring only once .
Second method I used was
CGAffineTransform transforms = CGAffineTransformRotate(imgView.transform, M_PI/2);
imgView.transform = transforms;
Now the result what I get is the image in Image view is continusely rotating in each move. But imageview is not rotating. What i need is to rotate the imageview not the image.
Any help will be greatly appreciated.
Thanks & Best Regards,
Rupesh R Menon
If I understood correctly you want to achieve single finger rotation on the image. If it is so you can use following functions from one of my live working project. You can use this for single image as well as multiple images. You need to modify at some extend for multiple images. Best way is extend UIImageView class and create your own class.
From touches moved call this function
[self transformImagewithTouches:touch];
Declare 1 property and synthesize as follows. (Also please declare other variables if required in below code.
#property (nonatomic) CGFloat fltRotatedAngle;
-(void)transformImagewithTouches:(UITouch *)touchLocation
{
//NSLog(#"Before %f",self.fltRotatedAngle);
CGPoint touchLocationpoint = [touchLocation locationInView:[self superview]];
CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:[self superview]];
CGPoint origin;
origin.x=self.center.x;
origin.y=self.center.y;
CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
CGAffineTransform newTransform =CGAffineTransformScale(self.transform, 1, 1);
CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
CGFloat newAngle = currentRotation- previousRotation;
//Calculate Angle to Store
fltTmpAngle = fltTmpAngle+newAngle;
self.fltRotatedAngle = (fltTmpAngle*180)/M_PI;
newTransform = CGAffineTransformRotate(newTransform, newAngle);
[self animateImageView:self toPosition:newTransform];
}
-(void)animateImageView:(UIImageView *)theView toPosition:(CGAffineTransform) newTransform
{
[UIView setAnimationsEnabled:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.0750];
self.transform = newTransform;
[UIView commitAnimations];
}
-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
CGFloat x = secondPoint.x - firstPoint.x;
CGFloat y = secondPoint.y - firstPoint.y;
CGPoint result = CGPointMake(x, y);
return result;
}
Hope it helps. If you stuck up please let me know I ll definitely help you.
Thanks for the interest shown by you in helping me.
When I used the following code I was able to rotate the image view in my desired angle.
imgView.layer.transform = CATransform3DMakeRotation(pCalculator.tangentToCornerAngle, 0, 0, 1);
where imgView is an UIImageView.
pCalculator.tangentToCornerAngle is the desired angle in which rotation has to be made.
Function is CATransform3DMakeRotation(CGFloat angle, CGFloat x, <CGFloat y, CGFloat z);
Thankyou All,
Rupesh R Menon
It's because you have rotated it by M_PI/2 and when you rotate it again it rotates it from the initial position. Use this one
CGAffineTransform transforms = CGAffineTransformConcat(imgView.transform,CGAffineTransformMakeRotation(M_PI/2));
imgView.transform = transforms;

Need to get frame co-ordinate of image while animation

I am using an app, in that am image is animating from top to bottom of screen using the animation.
code:
- (void)onTimer
{
// build a view from our flake image
flakeView = [[UIImageView alloc] initWithImage:flakeImage];
flakeView.backgroundColor=[UIColor blueColor];
// use the random() function to randomize up our flake attributes
int startX =round(random() % 460);
printf("\n ===== startX :%d",startX);
int endX = round(random() % 460);
printf("\n ===== endX :%d",endX);
double scale = 1 / round(random() % 100) - 1.0;
double speed = 1 / round(random() %100) + 1.0;
// set the flake start position
flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
flakeView.alpha = 1.0;
// put the flake in our main view
[mView addSubview:flakeView];
[UIView beginAnimations:nil context:flakeView];
[UIView setAnimationDuration:20 * speed];
// code to get current position of image on view
CALayer mainLayer = flakeView.layer.presentationLayer;
CGRect layerFrame = mainLayer.frame;
BOOL intersects = CGRectIntersectsRect(layerFrame, dragger.frame);
// end position on screen
flakeView.frame = CGRectMake(endX, 500.0, 15.0 * scale, 15.0 * scale);
// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:#selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
- (id)init
{
if(self = [super init])
{
mView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,440)];
mView.backgroundColor = [UIColor whiteColor];
mainLayer =[CALayer layer];
// load our flake image we will use the same image over and over
flakeImage = [UIImage imageNamed:#"apple.png"];
[mView.layer addSublayer:mainLayer];
// start a timet that will fire 20 times per second
[NSTimer scheduledTimerWithTimeInterval:(0.8) target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
self.view=mView;
}
return self;
}
I used this to get position of an image that is animating from top to bottom of the screen.
but I am getting the connstant values of x and y for the image that is animating.
Can any one help in this.
Thank U.
One thing I have noted from above code is that you are adding flakeView each time on mView. Do you want so? Please try with following code.
- (void)onTimer
{
int startX =round(random() % 460);
printf("\n ===== startX :%d",startX);
int endX = round(random() % 460);
printf("\n ===== endX :%d",endX);
double scale = 1 / round(random() % 100) - 1.0;
double speed = 1 / round(random() %100) + 1.0;
if(flakeView == nil)
{
// build a view from our flake image
flakeView = [[UIImageView alloc] initWithImage:flakeImage];
flakeView.backgroundColor=[UIColor blueColor];
// use the random() function to randomize up our flake attributes
// set the flake start position
flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
flakeView.alpha = 1.0;
// put the flake in our main view
[mView addSubview:flakeView];
}
[UIView beginAnimations:nil context:flakeView];
[UIView setAnimationDuration:20 * speed];
// code to get current position of image on view
CALayer mainLayer = flakeView.layer.presentationLayer;
CGRect layerFrame = mainLayer.frame;
BOOL intersects = CGRectIntersectsRect(layerFrame, dragger.frame);
// end position on screen
flakeView.frame = CGRectMake(endX, 500.0, 15.0 * scale, 15.0 * scale);
// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:#selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
NSLog(#"Co-ordinates of imageview : (%f, %f, %f, %f) %#",flakeView.frame.origin.x,flakeView.frame.origin.y,flakeView.frame.size.width,flakeView.frame.size.height);
}
Thanks,
I'm wondering why you could use the dot-notation to access the presentationLayer. It is a method, not a property of CALayer.
Note: "Use of [UIView beginAnimations:context:] is discouraged in iPhone OS 4.0 and later. You should use the block-based animation methods instead." (Apple Docs). If you don't know what block-based animations are, read: What are block-based animation methods in iPhone OS 4.0?
If it still doesn't work with block-based animations, try to set the layer's delegate to nil or use CABasicAnimation either implicitly or explicitly.

iPhone SDK: Flip-and-scale animation between view controllers using blocks?

I'm trying to create a flip-and-scale animation between two view controllers. This seems possible using animation blocks available in iOS 4.0, but I'm still unsure how to implement it. I found this SO question which shows a flip animation.
Using this code, flipping between two views works fine, but scaling doesn't -- the flip animation completes and then the new view jumps to the correct size. How would I flip the view and scale it at the same time?
UIView *tempContainer = myView.contentView ;
[UIView transitionWithView:tempContainer
duration:2
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[[[tempContainer subviews] objectAtIndex:0] removeFromSuperview];
[tempContainer addSubview:myOtherViewController.view];
CGAffineTransform transform = CGAffineTransformMakeScale(4.0, 4.0);
tempContainer.transform = transform;
}
completion:^(BOOL finished){
[tempContainer release];
}];
I suppose this happens because the option UIViewAnimationOptionTransitionFlipFromRight somehow overrides everything else. Try using nesting animations or link them together
Here's how I flip and scale between two views of different sizes. I break the animation into a few parts. First I put the back view at the same location as the front view, but make it hidden. I then flip and scale the front view halfway. The back view is given the same transform as the front view, then rotated and scaled the rest of the way. Flipping back is basically the reverse.
I suppose you could use a different view controllers view property as the back view, but I haven't tried that.
// flip and scale frontView to reveal backView to the center of the screen
// uses a containerView to mark the end of the animation
// parameterizing the destination is an exercise for the reader
- (void)flipFromFront:(UIView*)frontView toBack:(UIView*)backView destination:(CGRect)destRect
{
float duration = 0.5;
// distance from center of screen from frontView
float dx = destRect.origin.x + CGRectGetWidth(destRect) / 2 - frontView.center.x;
float dy = destRect.origin.y + CGRectGetHeight(destRect) / 2 - frontView.center.y;
float scale = CGRectGetWidth(destRect) / CGRectGetWidth(frontView.bounds);
// this prevents any tearing
backView.layer.zPosition = 200.0;
// hide the backView and position where frontView is
backView.hidden = NO;
backView.alpha = 0.0;
backView.frame = frontView.frame;
// start the animation
[UIView animateKeyframesWithDuration:duration
delay:0.25
options:UIViewKeyframeAnimationOptionCalculationModeCubic
animations:^{
// part 1. Rotate and scale frontView halfWay.
[UIView addKeyframeWithRelativeStartTime:0.0
relativeDuration:0.5
animations:^{
// get the transform for the blue layer
CATransform3D xform = frontView.layer.transform;
// translate half way
xform = CATransform3DTranslate(xform, dx/2, dy/2, 0);
// rotate half way
xform = CATransform3DRotate(xform, M_PI_2, 0, 1, 0);
// scale half way
xform = CATransform3DScale(xform, scale/2, scale/2, 1);
// apply the transform
frontView.layer.transform = xform;
}];
// part 2. set the backView transform to frontView so they are in the same
// position.
[UIView addKeyframeWithRelativeStartTime:0.5
relativeDuration:0.0
animations:^{
backView.layer.transform = frontView.layer.transform;
backView.alpha = 1.0;
}];
// part 3. rotate and scale backView into center of container
[UIView addKeyframeWithRelativeStartTime:0.5
relativeDuration:0.5
animations:^{
// undo previous transforms with animation
backView.layer.transform = CATransform3DIdentity;
// animate backView into new location
backView.frame = destRect;
}];
} completion:^(BOOL finished) {
self.displayingFront = !self.displayingFront;
}];
}
// flip from back to front
- (void) flipFromBack:(UIView*)backView toFront:(UIView*)frontView
{
float duration = 0.5;
// get distance from center of screen to destination
float dx = backView.center.x - frontView.center.x;
float dy = backView.center.y - frontView.center.y;
backView.layer.zPosition = 200.0;
frontView.hidden = YES;
// this is basically the reverse of the previous animation
[UIView animateKeyframesWithDuration:duration
delay:0
options:UIViewKeyframeAnimationOptionCalculationModeCubic
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0
relativeDuration:0.5
animations:^{
CATransform3D xform = backView.layer.transform;
xform = CATransform3DTranslate(xform, -dx/2, -dy/2, 0);
xform = CATransform3DRotate(xform, M_PI_2, 0, 1, 0);
xform = CATransform3DScale(xform, 0.75, 0.75, 1);
backView.layer.transform = xform;
}];
[UIView addKeyframeWithRelativeStartTime:0.5
relativeDuration:0.0
animations:^{
backView.alpha = 0.0;
frontView.hidden = NO;
}];
[UIView addKeyframeWithRelativeStartTime:0.5
relativeDuration:0.5
animations:^{
self.hiddenView.alpha = 0.0;
frontView.layer.transform = CATransform3DIdentity;
}];
} completion:^(BOOL finished) {
self.displayingFront = !self.displayingFront;
}];
}