How can i make transition effects in UIImageView - ios

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

Related

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 make an image grow in size during an animation

Basically, I am trying to simulate an explosion that starts small but grows until it takes up the entire screen.
-(void)Dying{
[BirdMovement invalidate];
[TunnelMovement invalidate];
[TunnelMovement2 invalidate];
[TunnelMovement3 invalidate];
[TunnelMovement4 invalidate];
[BirdMovement_orange invalidate];
[BirdMovement_yellow invalidate];
[BirdMovement_green invalidate];
[BirdMovement_blue invalidate];
[self.window makeKeyAndVisible];
Bird.frame=CGRectMake(Bird.center.x,Bird.center.y,23,37);
Bird.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Explode1.png"],
[UIImage imageNamed:#"Explode2.png"],
[UIImage imageNamed:#"Explode3.png"],
[UIImage imageNamed:#"Explode4.png"]
, nil];
Bird.animationDuration = 3;
Bird.animationRepeatCount = 1;
[Bird startAnimating];
[self.window addSubview:Bird];
[self performSelector:#selector(Dead) withObject:nil afterDelay:2.0];
}
-(void)Dead{
[Bird stopAnimating];
[self GameOver];
}
I tried messing with the CGRectMake saying things like
[Bird.frame=CGRectMake(Bird.center.x,Bird.center.y,23 +5 ,37 +5);]
But obviously this wont work cause its just making it (28, 42).
This will animate your imageView to your new size over 3 seconds.
UIImageView * bird = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
bird.animationImages = #[
[UIImage imageNamed:#"bird1"],
[UIImage imageNamed:#"bird2"],
[UIImage imageNamed:#"bird3"],
[UIImage imageNamed:#"bird4"],
[UIImage imageNamed:#"bird5"]
];
[self.view addSubview:bird];
[bird startAnimating];
[UIView animateWithDuration:2 animations:^{
bird.frame = CGRectMake(0, 0, 200, 200);
}];

Switching images using NSTimer

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

Animation and ScrollView

I am currently trying to build an image viewer that moves to the next photo every 30 seconds, which also allows to swipe left / right to the next/previous photo manually.
So far I have built two separate apps, one with the swiping functionality and the second with the timer. I have tried many tutorials on the web and am looking for someone to point me in the right direction.
This code switches images every thirty seconds
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *animatedImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 55, 400, 550)];
[self.view addSubview:animatedImageView];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"IMG_0052.JPG"],
[UIImage imageNamed:#"IMG_0054.JPG"],
[UIImage imageNamed:#"IMG_0081.JPG"],
nil];
animatedImageView.animationDuration = 30.0 * [animatedImageView.animationImages count];
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];
}
This second piece of code has the swiping functionality, I am looking to combine the two
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image1 = [UIImage imageNamed:#"workExample.jpg"];
UIImage *image2 = [UIImage imageNamed:#"IMG_0054.JPG"];
UIImage *image3 = [UIImage imageNamed:#"IMG_0052.JPG"];
imageArray = [NSArray arrayWithObjects:image1, image2, image3, nil];
for (int i = 0; i < [imageArray count]; i++) {
//This is to create a imageview for every single pagecontrol
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [imageArray objectAtIndex:i];
[self.scrollView addSubview:imageView];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imageArray count], scrollView.frame.size.height);
}
I am not using a scrollview and implementing in a different way. I hope it suits your needs
In .h
#interface ViewController : UIViewController
{
NSMutableArray *imagesToAnimate;
UIImageView *animatedImageView;
}
#property (strong, nonatomic) IBOutlet UIView *imageGalleryView;
Im viewDidLoad
animatedImageView = [[UIImageView alloc] initWithFrame:self.imageGalleryView.bounds];
imagesToAnimate = [NSMutableArray arrayWithArray:[NSArray arrayWithObjects:
[UIImage imageNamed:#"IMG_0052.JPG"],
[UIImage imageNamed:#"IMG_0054.JPG"],
[UIImage imageNamed:#"IMG_0081.JPG"],
nil]];
[self.imageGalleryView addSubview:animatedImageView];
//Start a timer
[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(changeImge) userInfo:nil repeats:YES];
animatedImageView.image = imagesToAnimate[0];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(gallerySwiped)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imageGalleryView addGestureRecognizer:swipeGesture];
//Give the gallery a border
self.imageGalleryView.layer.borderColor = [UIColor grayColor].CGColor;
self.imageGalleryView.layer.borderWidth = 2.0f;
self.imageGalleryView.layer.cornerRadius = 15.0f;
Now function to update the image as as per scheduled time
-(void)changeImge
{
int index = [imagesToAnimate indexOfObject:animatedImageView.image];
index++;
//this is for the left to right animation ;
CATransition *transition = [CATransition new];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
if (index > imagesToAnimate.count-1) {
animatedImageView.image = imagesToAnimate [0];
}else {
animatedImageView.image = imagesToAnimate[index];
}
[animatedImageView.layer addAnimation:transition forKey:#"transition"];
}
this would handle the swipe
-(void)gallerySwiped
{
[self changeImge];
}
To stop the timer store it in a global variable and call invalidate.I hope this helps.

How to swipe NSArray of images in Order using ISwipeGesture iOS 7

Hi in my application i have array of images i want to view image by swipe so i have used the Swipe Gesture in my application for both left and right but the problem is its not showing the images in order its just showing two images only please tell me how to resolve this issue.
My swipe code.
- (IBAction)handleswipe:(UIGestureRecognizer *)sender {
//NSLog(#"swiped");
int imageIndex = 3;
NSArray *images = [[NSArray alloc] initWithObjects:
#"2.jpg",
#"3.jpg",
#"4.jpg",
#"5.jpg", nil];
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch (direction) {
case UISwipeGestureRecognizerDirectionLeft:
imageIndex++;
break;
case UISwipeGestureRecognizerDirectionRight:
imageIndex--;
break;
default:
break;
}
imageIndex = (imageIndex < 0 )? ([images count] -1):
imageIndex % [images count];
imageview.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}
I have added the Swipe Gesture in image view and i have set one image already in imageview but its not showing in order its only showing two images in randomly please tell me where I'm doing wrong and how to resolve this one.
Thanks.
right_gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(method)];
right_gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[objSecondView.view addGestureRecognizer:right_gesture];
right_gesture.delegate = self;
and in method...
if (varSecondCount==3) {
}
else{
varSecondCount++;
switch (varSecondCount) {
case 0:
[imageView setImage:[UIImage imageNamed:#"rcz_img1.png"]];
break;
case 1:
[imageView setImage:[UIImage imageNamed:#"rcz_img2.png"]];
break;
case 2:
[imageView setImage:[UIImage imageNamed:#"rcz_img3.png"]];
break;
case 3:
[imageView setImage:[UIImage imageNamed:#"rcz_img4.png"]];
break;
case 4:
[imageView setImage:[UIImage imageNamed:#"step2_5.png"]];
break;
default:
break;
}
hope this helps....
I have implemented same thing in my app here is code for that: imageIndxMain is global variable holding current index of array
if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
{
if (imageIndxMain==0)
return;
else
{
CATransition *transition = [CATransition animation];
transition.duration = .40;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate = self;
[imagescrollview.layer addAnimation:transition forKey:nil];
UIImage *img1;
imageIndxMain--;
ImgView.image=[imageArray objectAtIndex:imageIndxMain];
}}else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
{
if (imageIndxMain == ([imageArray count] - 1))
{
return;
}
else
{
CATransition *transition = [CATransition animation];
transition.duration = .40;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromRight;
transition.delegate = self;
[imagescrollview.layer addAnimation:transition forKey:nil];
UIImage *img1;
imageIndxMain++;
ImgView.image=[imageArray objectAtIndex:imageIndxMain];
}
}

Resources