Switching images using NSTimer - ios

I have couple of images i want to switch. There is a simple function i made for do this:
-(void)imageSlide{
if (!isMoved){
CGRect frame = self.imageSlideshow.frame;
frame.origin.x = self.imageSlideshow.frame.origin.x - 320;
self.imageSlideshow.frame = frame;
NSLog(#"1 caze work");
isMoved = YES;
}
if (isMoved){
CGRect frame = self.imageSlideshow.frame;
frame.origin.x = self.imageSlideshow.frame.origin.x + 320;
self.imageSlideshow.frame = frame;
NSLog(#"2 caze work");
isMoved = NO;
}
}
There is NSTimer which call that function:
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(imageSlide)
userInfo:nil
repeats:YES];
BOOL isMoved; placed in implementation of class.
What i want is, to remove an image and then, shown again (and repeat it every 2 seconds). It would be nice to have smooth animation as well.
That code:
for (int i=0; i<99; i++){
self.imageSlideshow.image = [UIImage imageNamed:(i % 2) ? #"ipd1.jpg" : #"ipd2.jpg"];
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[self.imageSlideshow.layer addAnimation:transition forKey:nil];
}
Also not working, no idea why. Image stand still. I did import quartz library and include headers.

you can acheive this by using this code :-
#import <QuartzCore/QuartzCore.h>
...
imageView.image = [UIImage imageNamed:(i % 2) ? #"3.jpg" : #"4.jpg"];
CATransition *transition = [CATransition animation];
transition.duration = 2.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[imageView.layer addAnimation:transition forKey:nil];
and NSTimer Job should be done here by transition.duration. So, no need of NSTimer anymore here.
Courtesy :- https://stackoverflow.com/a/2834693/1865424
This piece of code works too :-
// create the view that will execute our animation
UIImageView* imageSlideshow = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
imageSlideshow.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#“ipd1.jpg"],
[UIImage imageNamed:#“ipd2.jpg"], nil];
// all frames will execute in 1.75 seconds
imageSlideshow.animationDuration = 1.75;
// repeat the animation forever
imageSlideshow.animationRepeatCount = 0;
// start animating
[imageSlideshow startAnimating];
// add the animation view to the main window
[self.view addSubview:imageSlideshow];

I think you need:
[UIView animateWithDuration:1 options:UIViewAnimationOptionCurveEaseIn
animations:^{
//Change frame here.
} completion:^ (BOOL completed) {}
];

Try this code. It is worked for me.
#pragma mark -
#pragma mark viewDidAppear
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
updateBCK = [NSTimer scheduledTimerWithTimeInterval:(4.0) target:self selector:#selector(changeImage) userInfo:nil repeats:YES];
[updateBCK fire];
}
#pragma mark -
#pragma mark viewDidDisAppear
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if ([updateBCK isValid]){
[updateBCK invalidate];
updateBCK = nil;
}
}
-(void)changeImage{
static int i=0;
if (i == [myImages count]){
i=0;
}
[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:0.6];
backgroundImageView.alpha=1;
backgroundImageView.image =[myImages objectAtIndex:i];
[UIImageView commitAnimations];
i++;
}

It is bug in your code. After setting isMoved = YES, you can't check if isMoved == YES.
I don't know if that is what you mean, but try this code:
-(void)imageSlide{
[UIView animateWithDuration:1 animations:^{
if (!isMoved){
CGRect frame = self.imageSlideshow.frame;
frame.origin.x = self.imageSlideshow.frame.origin.x - 320;
self.imageSlideshow.frame = frame;
NSLog(#"1 caze work");
isMoved = YES;
}
else
{
CGRect frame = self.imageSlideshow.frame;
frame.origin.x = self.imageSlideshow.frame.origin.x + 320;
self.imageSlideshow.frame = frame;
NSLog(#"2 caze work");
isMoved = NO;
}
} completion:^(BOOL finished) {
}];
}
EDIT
Maybe this code will help you:
-(void)slideshow
{
static int i = 0;
UIImage * img = [UIImage imageNamed:(i % 2) ? #"ipd1.jpg" : #"ipd2.jpg"];
[UIView transitionWithView:self.imageSlideshow duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.imageSlideshow.image = img;
} completion:^(BOOL finished) {
i++;
[self performSelector:#selector(slideshow) withObject:nil afterDelay:2.0];
}];
}

Related

Open/Close view as menu

I'm trying to do an effect to open and close a view(menu) the view has origin x = 55.
the open effect works properly good, but the close effect has an problem:
the effect begins at x = 0 and my view is at x = 55...then the effect appears strange....
There's my code:
-(IBAction)menuClick:(id)sender {
if(!self.viewMais) {
CGRect screen = [[UIScreen mainScreen]bounds];
self.viewMais = [[UIView alloc]initWithFrame:CGRectMake(55, 0, screen.size.width-55, screen.size.height)];
[self.viewMais setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:self.viewMais];
[self open];
}
else {
[self close];
}
}
-(void)open {
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromRight;
UIView *containerView = self.viewMais;
[containerView.layer addAnimation:transition forKey:nil];
}
-(void)close {
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromLeft;
UIView *containerView = self.viewMais;
[containerView.layer addAnimation:transition forKey:nil];
[self performSelector:#selector(removerViewMais) withObject:nil afterDelay:0.3];
}
-(void) removerViewMais {
[self.viewMais removeFromSuperview];
self.viewMais = nil;
}
-(IBAction)showPicker{
if (!isPickerShow) {
[self.view endEditing:YES];
[UIView animateWithDuration:0.3 animations:^{
viewWithPicker.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-viewWithPicker.frame.size.height, 320, 555);
isPickerShow=YES;
}];
}
}
-(IBAction)hidePicker{
if (isPickerShow) {
[UIView animateWithDuration:0.3 animations:^{
viewWithPicker.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height , 320, 255);
isPickerShow=NO;
}];
}
}
I solved my problem with the following code in close menu
-(void)close {
CGRect destination = self.view.frame;
[UIView animateWithDuration:0.25 animations:^{
self.viewMais.frame = CGRectMake(destination.size.width, self.viewMais.frame.origin.y, self.viewMais.frame.size.width, self.viewMais.frame.size.height);
} completion:^(BOOL finished) {
[self.viewMais setHidden:YES];
[self.viewMais removeFromSuperview];
self.viewMais = nil;
}];
}

Infinite animation for image changes in imageView ios

I want to implement the next situation: step by step change image in uiImageView with UIViewAnimationOptionTransitionCrossDissolve animation.
I've tried the following:
[UIView transitionWithView:imageView
duration:2.0f
options: UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
imageView.image = [self.images objectAtIndex:self.currentIndex];
self.currentIndex ++;
if (self.currentIndex >2) self.currentIndex = 0;
} completion:^(BOOL finished) {
}];
But this does not even start animation.
The only thing makes it work - use recursion for that purpose:
- (void) switchBckgroundWithIndex: (NSInteger) index imageView: (UIImageView*) imageView
{
if (index >=[self.images count]) index = 0;
#weakify(self)
[UIView transitionWithView:imageView
duration:2.0f
options: UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionTransitionCrossDissolve
animations:^{
#strongify(self)
imageView.image = [self.images objectAtIndex:index];
} completion:^(BOOL finished) {
#strongify(self)
[self switchBckgroundWithIndex:index+1 imageView:imageView];
}];
}
How can i change images repeatedly with those animation effect without using recursion? Please, help me to find solution
I have used this method for animating images in UIImageView
-(void)viewDidLoad
{
[super viewDidLoad];
mutArr=[[NSMutableArray alloc]init];
[mutArr addObject:#"b1#2x.png"];
[mutArr addObject:#"b2#2x.png"];
[mutArr addObject:#"b3#2x.png"];
[mutArr addObject:#"b4#2x.png"];
NSTimer *timerForImage = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(animateImages) userInfo:nil repeats:YES];
}
-(void)animateImages
{
imageCount = imageCount + 1;
NSString *imageName =[NSString stringWithFormat:#"%#",[mutArr objectAtIndex:imageCount]];
if (imageCount==4)
{
imageCount=0;
}
_imgVw.image = [UIImage imageNamed:imageName];
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[_imgVw.layer addAnimation:transition forKey:nil];
}
#interface abc ()
{
int imgCount;
}
then in -
- (void)viewDidLoad
{
[super viewDidLoad];
imgCount = 0;
[self animateAllImages];
}
- (void)animateAllImages
{
NSArray *animationImages = #[[UIImage imageNamed:#"Image1"], [UIImage imageNamed:#"Image2"],[UIImage imageNamed:#"Image3"],[UIImage imageNamed:#"Image4"]];
UIImage *image = [animationImages objectAtIndex:(imgCount % [animationImages count])];
[UIView transitionWithView:self.animationImageView
duration:0.8f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.animationImageView.image = image;
} completion:^(BOOL finished) {
[self animateAllImages];
imgCount++;
}];
}

How to apply animation during switching images

I have an imageView in top of my screen, and what i want is - to switch images with specific time interval, and to make it with animation (I'm not specify what really animation should be, that could be fade for example). I have suggested to add that code snippet:
// load all the frames of our animation
self.imageSlideshow.animationImages = [NSArray arrayWithObjects:
self.firstImageIps, self.secondImageIps, self.thirdImageIps, self.fourthImageIps, self.fifthImageIps, nil];
// all frames will execute in 1.75 seconds
self.imageSlideshow.animationDuration = 1.75;
// repeat the animation forever
self.imageSlideshow.animationRepeatCount = 0;
// start animating
NSLog(#"that one called?");
[self.imageSlideshow startAnimating];
But images here only switching, without any animation.
And here is more serious issues - if i set duration to more, then 3 seconds, it start act weird (not of all images show, first image show more often then second and third. It look like it can't achieve a full cycle and move to first image after some short time again and again).
How to achieve switching images with animation, with timer interval, for example 5-7 seconds?
Any advice would be appreciated, thanks!
Try this one:
- (void)viewDidLoad {
[super viewDidLoad];
self.imgArr = [[NSArray alloc] initWithObjects:#"DSC06715.JPG",#"Snapshot_20121113_18.JPG",#"IMAG1689.jpg",#"IMAG1720.jpg",#"IMAG1396.jpg", nil];
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:#selector(performTransition) userInfo:nil repeats:YES];
}
-(void)performTransition
{
int randImg = arc4random()%self.imgArr.count;
[self.imageView1 setImage:[UIImage imageNamed:[self.imgArr objectAtIndex:randImg]]];
CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
NSString *types[4] = {kCATransitionFade};
transition.type = types[1];
transition.delegate = self;
[self.imageView1.layer addAnimation:transition forKey:nil];
}
-(void)animateImages
{
count++;
[UIView transitionWithView:imageSlideshow
duration:2.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
imageSlideshow.image = [imagesArray objectAtIndex: count % [imagesArray count]];
} completion:^(BOOL finished) {
[self animateImages];
}];
}
This code snippet will help you to add any type of animation.
One of the simplest ways to do animation.
Code example :
[UIView animateWithDuration:0.3 animations:^{
//change alpha with animation.
self.friendsTableView.alpha = 1.f;
} completion:^(BOOL finished) {
}];
This animation could be set with delay and optionsas well. As far as i know it is the simplest way to set proper animation with less afford.
To make animation between 2 view you may use spacial UIView method:
- (void)makeCrossDissolveAnimation {
NSArray *images = self.imageSlideshow.animationImages;
static int counter = 0;
UIImageView *startView = [[UIImageView alloc] initWithImage:images[counter]];
counter = (counter+1) % [images count];
UIImageView *endView = [[UIImageView alloc] initWithImage:images[counter]];
[UIView transitionFromView:startView toView:endView duration:ELAnimationDuration
options:UIViewAnimationOptionTransitionCrossDissolve
completion:^(BOOL finished) {
// Your completion block
}];
}

How can i make transition effects in UIImageView

I have a set of images which i want to display as a slideshow.Is there any transition effects i can apply on UIImageView.
[imgView setImage:[UIImage imageNamed: #"images.jpeg"]];
Yes you can apply transition effects on UIImageView.
First you need to import QuartzCore framework.
#import <QuartzCore/QuartzCore.h>
Then you can use the CoreAnimation as below:
[imgView setImage:[UIImage imageNamed: #"images.jpeg"]];
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[imgView.layer addAnimation:transition forKey:nil];
Or you can simply animate a group of images in UIImageView as
imgView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.jpeg"],
[UIImage imageNamed:#"image2.jpeg"],
[UIImage imageNamed:#"image3.jpeg"],
[UIImage imageNamed:#"image4.jpeg"], nil];
imgView.animationDuration = 1.0f;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
Here is a sample code of how to create a slide show. Make sure you import some images unto your project and in the case method change the wall images to the name of your images.
- (IBAction)start:(id)sender {
_button.hidden = YES;
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(photoCounter) userInfo:nil repeats:YES];
}
- (void)photoCounter {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.90];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:NO];
[self updatePhoto];
[UIView commitAnimations];
}
- (void)updatePhoto
{
switch (imageCount)
{
case 0:
_images.image = [UIImage imageNamed:#"wall1.jpg"];
break;
case 1:
_images.image = [UIImage imageNamed:#"wall2.jpg"];
break;
case 2:
_images.image = [UIImage imageNamed:#"wall3.jpg"];
break;
case 3:
_images.image = [UIImage imageNamed:#"wall4.jpg"];
break;
case 4:
_images.image = [UIImage imageNamed:#"wall5.jpg"];
break;
default:
break;
}
imageCount ++;
if (imageCount > 4)
imageCount = 0;
}
Try with below code:
NSArray *imageNames = #[#"2_1014.png", #"2_1015.png", #"2_1016.png", #"2_1017.png",#"2_1018.png", #"2_1019.png", #"2_1020.png", #"2_1021.png",#"2_1022.png", #"2_1023.png", #"2_1024.png", #"2_1025.png",#"2_1026.png", #"2_1027.png", #"2_1028.png",
#"2_1029.png",#"2_1030.png",#"2_1030.png",#"2_1029.png",
#"2_1028.png",#"2_1027.png",#"2_1026.png",#"2_1025.png",
#"2_1024.png",#"2_1023.png",#"2_1022.png",#"2_1021.png",
#"2_1020.png",#"2_1019.png",#"2_1018.png",#"2_1017.png",
#"2_1016.png",#"2_1015.png",#"2_1014.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
animationImageView.animationImages = images;
animationImageView.animationRepeatCount=0;
animationImageView.animationDuration = 1;
[self.view addSubview:animationImageView];
[animationImageView startAnimating];

Push Segue animation without UINavigationController

I'm trying to add a simple push animation between UIViewControllers using a custom segue.
(Without converting my controllers to use a UINavigationController)
The answers found so far work fine with a navigation controller, but fail when not using a navigation controller. (I am still reading and trying other answers I've seen here on stack-overflow)
My custom segue .m thanks to (ZHCustomSegue.m) Zakir on 7/5/12
#import "SFCustomSegue.h"
#import "QuartzCore/QuartzCore.h"
#implementation SFCustomSegue
-(void)perform {
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = 4.0;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[sourceViewController.view.layer addAnimation:transition forKey:kCATransition];
[sourceViewController presentViewController:[self destinationViewController] animated:NO completion:nil];
}
If I use a navigation controller as per Zakir's original example and replace the last 2 lines with:
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
It works....
Apple's doc says (notice not a navigation controller, and using a modal segue):
- (void)perform
{
// Add your own animation code here.
[[self sourceViewController] presentModalViewController:[self destinationViewController] animated:NO];
}
By NOT Working, I get no animation. In Zakir's code, using 4.0 seconds it takes 4.0 seconds and I get the animation I want. Of course, if I use a navigation controller a push tranition is the default. In this code, I get no animation and it takes 0 seconds.
Has anyone gotten custom segue animations to work with UIViewControllers?
Since you are calling presentViewController, the source view controller is sticking around. If you actually want to replace the source view controller with the destination view controller, so that the source gets dealloced, you can do this:
static UIImageView *screenShotOfView(UIView *view)
{
// Create a snapshot for animation
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImageView *screenShot = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
return screenShot;
}
- (void)perform
{
UIViewController *source = (UIViewController *) self.sourceViewController;
UIViewController *destination = (UIViewController *) self.destinationViewController;
// Swap the snapshot out for the source view controller
UIWindow *window = source.view.window;
UIImageView *screenShot = screenShotOfView(source.view);
CGRect originalFrame = destination.view.frame;
BOOL animsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
{
window.rootViewController = destination;
[window addSubview:screenShot];
[source.view removeFromSuperview];
CGRect frame = destination.view.frame;
frame.origin.x += source.view.bounds.size.width;
destination.view.frame = frame;
}
[UIView setAnimationsEnabled:animsEnabled];
[UIView animateWithDuration:kAnimationDuration
animations:^{
destination.view.frame = originalFrame;
CGRect frame = screenShot.frame;
frame.origin.x -= screenShot.bounds.size.width;
screenShot.frame = frame;
}
completion:^(BOOL finished) {
[screenShot removeFromSuperview];
}];
}
It appears that my above code, is "nearly" working, but not quite.
The main issue at hand is likely when the final step is called.
My working code looks like this:
#import "SFCustomSegue.h"
#import "QuartzCore/QuartzCore.h"
#implementation SFCustomSegue
static const float delay = 0.5f;
-(void)perform {
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
[sourceViewController.view addSubview:destinationController.view];
CATransition* transition = [CATransition animation];
transition.duration = delay;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[sourceViewController.view.layer addAnimation:transition forKey:kCATransition];
[self performSelector:#selector(performDone:) withObject:destinationController afterDelay:delay];
}
- (void)performDone:(id)viewController{
UIViewController *destination = (UIViewController*)viewController;
[destination.view removeFromSuperview];
[[self sourceViewController] presentViewController:destination animated:NO completion:nil];
}
#end
The key to resolving this was adding the view for the transition (line 13), then removing it (line 28) and adding it back (line 29) using presentViewController:animated:completion:.
I am still "stuck" with using only the regular CATransition transition types, but that's good enough for me for now, since I want to use kCATransitionPush.
I have merged the ideas from both answers to get this code (working good):
(the answers from above don't count in the orientation of device so they won't work in landscape, for example)
(the variant with CATransitions wasn't appropriate because there was a gap between two views while the one pushed the another)
#import "PushSegue.h"
/*
#interface TransitionDelegate : NSObject
- (id) initWithScreenshot: (UIView*) screenshot;
#end
*/
#implementation PushSegue
- (void) perform
{
UIViewController* source = (UIViewController*) self.sourceViewController;
UIViewController* destination = (UIViewController*) self.destinationViewController;
const BOOL iPad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
float animationDuration = iPad ? 0.5 : 0.3;
// Swap the snapshot out for the source view controller
UIWindow* window = source.view.window;
UIImageView* screenShot = screenShotOfView(source.view);
// accord to device orientation
float rotation;
//NSString* transitionType;
CGRect originalFrame = destination.view.frame;
CGRect destinationFrame = destination.view.frame;
switch ([UIApplication sharedApplication].statusBarOrientation)
{
case UIInterfaceOrientationPortraitUpsideDown:
rotation = M_PI;
destinationFrame.origin.x -= source.view.bounds.size.width;
//transitionType = kCATransitionFromLeft;
break;
case UIInterfaceOrientationLandscapeLeft:
rotation = M_PI + M_PI_2;
destinationFrame.origin.y -= source.view.bounds.size.width;
//transitionType = kCATransitionFromBottom;
break;
case UIInterfaceOrientationLandscapeRight:
rotation = M_PI_2;
destinationFrame.origin.y += source.view.bounds.size.width;
//transitionType = kCATransitionFromTop;
break;
default:
rotation = 0;
destinationFrame.origin.x += source.view.bounds.size.width;
//transitionType = kCATransitionFromRight;
break;
}
screenShot.transform = CGAffineTransformMakeRotation(rotation);
// reposition after rotation
CGRect screenshotFrame = screenShot.frame;
screenshotFrame.origin.x = 0;
screenshotFrame.origin.y = 0;
screenShot.frame = screenshotFrame;
switch ([UIApplication sharedApplication].statusBarOrientation)
{
case UIInterfaceOrientationPortraitUpsideDown:
screenshotFrame.origin.x += screenShot.bounds.size.width;
break;
case UIInterfaceOrientationLandscapeLeft:
screenshotFrame.origin.y += screenShot.bounds.size.width;
break;
case UIInterfaceOrientationLandscapeRight:
screenshotFrame.origin.y -= screenShot.bounds.size.width;
break;
default:
screenshotFrame.origin.x -= screenShot.bounds.size.width;
break;
}
// swap the view with its screenshot
window.rootViewController = destination;
[window addSubview:screenShot];
[source.view removeFromSuperview];
/*
CATransition* transition = [CATransition animation];
transition.duration = animationDuration;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = transitionType;
transition.delegate = [[TransitionDelegate alloc] initWithScreenshot:screenShot];
[window addSubview:destination.view];
[screenShot.window.layer addAnimation:transition forKey:nil];
*/
const BOOL animationsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
{
destination.view.frame = destinationFrame;
}
[UIView setAnimationsEnabled:animationsEnabled];
[UIView animateWithDuration:animationDuration
animations:^
{
destination.view.frame = originalFrame;
screenShot.frame = screenshotFrame;
}
completion:^(BOOL finished)
{
[screenShot removeFromSuperview];
}];
/*
const BOOL animationsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
{
CGRect frame = destination.view.frame;
frame.origin.x += source.view.bounds.size.width;
destination.view.frame = frame;
}
[UIView setAnimationsEnabled:animationsEnabled];
[UIView animateWithDuration:animationDuration
animations:^
{
destination.view.frame = originalFrame;
CGRect frame = screenShot.frame;
frame.origin.x -= screenShot.bounds.size.width;
screenShot.frame = frame;
}
completion:^(BOOL finished)
{
[screenShot removeFromSuperview];
}];
*/
/*
CATransition* transition = [CATransition animation];
transition.duration = animationDuration;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
switch ([UIApplication sharedApplication].statusBarOrientation)
{
case UIInterfaceOrientationPortraitUpsideDown:
transition.subtype = kCATransitionFromLeft;
break;
case UIInterfaceOrientationLandscapeLeft:
transition.subtype = kCATransitionFromBottom;
break;
case UIInterfaceOrientationLandscapeRight:
transition.subtype = kCATransitionFromTop;
break;
default:
transition.subtype = kCATransitionFromRight;
break;
}
[source.view.window.layer addAnimation:transition forKey:nil];
[source presentViewController:destination animated:NO completion:nil];
*/
}
static UIImageView* screenShotOfView(UIView* view)
{
// Create a snapshot for animation
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImageView* screenShot = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
return screenShot;
}
#end
/*
#implementation TransitionDelegate
{
UIView* screenshot;
}
- (id) initWithScreenshot: (UIView*) screenshot
{
if (self = [super init])
{
self->screenshot = screenshot;
}
return self;
}
- (void) animationDidStop: (CAAnimation*) theAnimation
finished: (BOOL) flag
{
[screenshot removeFromSuperview];
}
#end
*/

Resources