Animations Back to back After Completion of one in objective c - ios

I want to do continuous animation in 3 imageviews back to back I have use the below code but it shows me animation in only first imageview.I debug Code, it will Go in method but it won't show Any animation in remaining.Below is my Code
-(void)imageNewsAnimation
{
[UIView animateWithDuration:0.35f animations:^{
_imgNews.animationImages=arrnews;
_imgNews.animationDuration=1;
_imgNews.animationRepeatCount=1;
_imgNews.image = [_imgNews.animationImages lastObject];
[_imgNews startAnimating];
} completion:^(BOOL finished) {
[_imgNews stopAnimating];
[self imageEventAnimation];
} ];
}
-(void)imageEventAnimation
{
[UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
_imgEvents.animationImages=arrEvents;
_imgEvents.animationDuration=1;
_imgEvents.animationRepeatCount=1;
_imgEvents.image = [_imgEvents.animationImages lastObject];
[_imgEvents startAnimating];
} completion:^(BOOL finished) {
[self imageDirectoryAnimation];
} ];
}
-(void)imageDirectoryAnimation
{
[UIView animateWithDuration:0.35f animations:^{
_imgDirectory.animationImages=arrdir;
_imgDirectory.animationDuration=1;
_imgDirectory.animationRepeatCount=1;
_imgDirectory.image = [_imgDirectory.animationImages lastObject];
[_imgDirectory startAnimating];
} completion:^(BOOL finished) {
[self imageInfoAnimation];
} ];
}
-(void)imageInfoAnimation
{
[UIView animateWithDuration:0.35f animations:^{
_imgInfo.animationImages=arrinfo;
_imgInfo.animationDuration=1;
_imgInfo.animationRepeatCount=1;
_imgInfo.image = [_imgInfo.animationImages lastObject];
[_imgInfo startAnimating];
} completion:^(BOOL finished) {
[self imageDirectoryAnimation];
} ];
}

Since startAnimating doesn't have a completion handler, I would set up something like this:
CycleImageView.h
#import <UIKit/UIKit.h>
typedef void (^CycleCompletion)(BOOL finished);
#interface CycleImageView : UIImageView
- (void)startAnimatingWithImages:(NSArray<UIImage *>*)images completion:(CycleCompletion)completion;
#end
CycleImageView.m
#import "CycleImageView.h"
#interface CycleImageView()
#property (nonatomic, copy) CycleCompletion completion;
#end
#implementation CycleImageView
- (void)startAnimatingWithImages:(NSArray<UIImage *>*)images completion:(CycleCompletion)completion {
self.completion = completion;
NSMutableArray *imageRefs = [NSMutableArray array];
for (NSInteger i = 0; i < images.count; i++) {
[imageRefs addObject:(id)images[i].CGImage];
}
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"contents"];
animation.delegate = self;
animation.calculationMode = kCAAnimationDiscrete;
animation.duration = 1;
animation.values = imageRefs;
animation.repeatCount = 1;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[self.layer addAnimation:animation forKey:#"animation"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if (self.completion != nil) {
self.completion(flag);
self.completion = nil;
}
}
#end
To use it:
Make imgNews, imgEvents, imgDirectory, and imgInfo all subclasses of CycleImageView. Then, using your supplied methods and variables, the implementation would change to this:
- (void)imageNewsAnimation {
[_imgNews startAnimatingWithImages:arrnews completion:^(BOOL finished) {
if (finished) {
[self imageEventAnimation];
}
}];
}
- (void)imageEventAnimation {
[_imgEvent startAnimatingWithImages:arrEvents completion:^(BOOL finished) {
if (finished) {
[self imageDirectoryAnimation];
}
}];
}
- (void)imageDirectoryAnimation {
[_imgDirectory startAnimatingWithImages:arrdir completion:^(BOOL finished) {
if (finished) {
[self imageInfoAnimation];
}
}];
}
- (void)imageInfoAnimation {
[_imgInfo startAnimatingWithImages:arrinfo completion:^(BOOL finished) {
if (finished) {
// NOTE: Are you intentionally creating this indefinite animation loop?
[self imageDirectoryAnimation];
}
}];
}
Edit:
Created a gist for CycleImageView, as well.

Perform animation with timer like
In viewDidLoad:
timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(imageAnimation:) userInfo:nil repeats:YES];
i = 0;
then
- (void)imageAnimation:(UIImage *) image {
i++;
if (i > 2) {
i = 0;
}
NSArray *arr = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"],[UIImage imageNamed:#"img3"]];
UIImage * toImage = [arr objectAtIndex:i];
[UIView transitionWithView:self.view
duration:2
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.imageview.image = toImage;
} completion:NULL];
}
Hope this help!

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

Animations in TableView in Ios

I have a tableview with a hidden property. I want to unhide it with animation so that it could slide from bottom to top? Any suggestions please.
-(IBAction) hideTable : (id) sender;
{
if(self.tb.hidden==true)
{
self.tb.alpha=1;
[self.tb setHidden:NO];
}
else{
[self.tb setHidden:YES];
}
}
-(IBAction) hideTable : (id) sender{
if (self.sessionView.hidden == YES) {
// Currently NOT Visible. Show it now
// First unhide it
yourTableView.hidden = NO;
[UIView animateWithDuration:0.5f
animations:^{
// Get the Existing table View frame
CGRect hiddenFrame = yourTableView.frame;
// Reset it to 0 or your own value
hiddenFrame.origin.y = 0;
// Set the new frame
yourTableView.frame = hiddenFrame;
} completion:^(BOOL finished) {
NSLog(#"Shown");
}];
}
else{
// Currently Visible. Hide it Now
[UIView animateWithDuration:0.5f
animations:^{
// Get the Existing table View frame
CGRect hiddenFrame = yourTableView.frame;
// Under the superview height
hiddenFrame.origin.y = yourTableView.superview.frame.size.height;
// Set the new frame
yourTableView.frame = hiddenFrame;
} completion:^(BOOL finished) {
yourTableView.hidden = YES;
NSLog(#"Hidden");
}];
}
}
try this
CGRect frame = self.tb.frame;
if (self.tb.hidden) {
CGRect temp_frame = frame;
frame.size.height = 1;
self.tb.frame = temp_frame;
self.tb.hidden = NO;
[UIView animateWithDuration:0.3
animations:^{
self.tb.frame = frame;
} completion:^(BOOL finished) {
}];
}
else
{
[UIView animateWithDuration:0.3
animations:^{
CGRect temp_frame = frame;
frame.size.height = 1;
self.tb.frame = temp_frame;
} completion:^(BOOL finished) {
self.tb.hidden = YES;
}];
}
try this
-(IBAction) hideTable : (id) sender;
{
if(self.tb.hidden==true)
{
self.tb.alpha=1;
[self.tb setHidden:NO];
[UIView animateWithDuration:.25
animations:^{
self.tb.frame = CGRectMake(0,50,320,400); // your req frame
}
];
}
else
{
[UIView animateWithDuration:0.35
animations:^{
self.tb.frame = CGRectMake(0, self.view.frame.size.height+10, 320,400); // y point of yor tableview is more than view bounds so it goes down
}
completion:^(BOOL finished){
[self.tb setHidden:YES];
}
];
}
}
You can use the below methods:
1. (void)showDropDownTable:- To show tableview with top to bottom animation.
2. (void)hideDropdownTable:- To hide tableview with bottom to top animation.
- (IBAction)btnDropDownClicked:(id)sender
{
if (checkShowHideTableView == TRUE)
{
[self hideTableView];
}
if (self.tblDropDown.hidden)
{
[self showDropDownTable];
}
else
{
[self hideDropdownTable];
}
}
- (void)showDropDownTable
{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationCurveEaseOut animations: ^{
_tblDropDown.frame = CGRectMake(self.tblDropDown.frame.origin.x, self.tblDropDown.frame.origin.y, self.tblDropDown.frame.size.width, kDropdownTableHeight);
self.tblDropDown.hidden = FALSE;
} completion: ^(BOOL finished) {
}];
}
- (void)hideDropdownTable
{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationCurveEaseOut animations: ^{
_tblDropDown.frame = CGRectMake(self.tblDropDown.frame.origin.x, self.tblDropDown.frame.origin.y, self.tblDropDown.frame.size.width, 0);
} completion: ^(BOOL finished) {
self.tblDropDown.hidden = TRUE;
}];
}
Hope this can help you.
Try the following
[self.tb beginUpdates];
[UIView animateWithDuration:0.5
delay:0.1
options:(UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
animations:^{
if(self.tb.isHidden)
{
self.tb.alpha=1;
[self.tb setHidden:NO];
}
else
{
self.tb.alpha=0;
[self.tb setHidden:YES];
}
}
completion:^(BOOL finished) {
}];
[self.tb endUpdates];

Unbalanced calls to begin/end appearance transitions for <UIViewController>

Based on the Apple documentation I came up with the following method to switch between controllers in a containment controller. When there is an oldC I am getting Unbalanced calls to begin/end appearance transitions for <...> on the console.
- (void) showController:(UIViewController*)newC withView:(UIView*)contentView animated:(BOOL)animated
{
UIViewController *oldC = self.childViewControllers.firstObject;
if (oldC == newC) {
return;
}
[oldC willMoveToParentViewController:nil];
[self addChildViewController:newC];
newC.view.frame = (CGRect){ 0, 0, contentView.frame.size };
[contentView addSubview:newC.view];
if (animated && oldC != nil) {
oldC.view.alpha = 1.0f;
newC.view.alpha = 0.0f;
[self transitionFromViewController:oldC toViewController:newC duration:0.25f options:0 animations:^{
oldC.view.alpha = 0.0f;
newC.view.alpha = 1.0f;
} completion:^(BOOL finished) {
[oldC removeFromParentViewController];
[newC didMoveToParentViewController:self];
}];
} else {
oldC.view.alpha = 0.0f;
newC.view.alpha = 1.0f;
[oldC removeFromParentViewController];
[newC didMoveToParentViewController:self];
}
}
This is how I call it:
- (IBAction) buttonSignIn:(id)sender
{
[self showController:self.signInViewController withView:self.contentView animated:(sender != nil)];
}
- (IBAction) buttonSignUp:(id)sender
{
[self showController:self.signUpViewController withView:self.contentView animated:(sender != nil)];
}
To track this down I am logging the appearance transitions
-(void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated
{
[super beginAppearanceTransition:isAppearing animated:animated];
NSLog(#"**begin %#", self);
}
-(void)endAppearanceTransition
{
[super endAppearanceTransition];
NSLog(#"**end** %#", self);
}
This is what the log looks like:
] **begin <SignInViewController: 0x10c769a20>
] **begin <SignUpViewController: 0x10c768770>
] Unbalanced calls to begin/end appearance transitions for <SignUpViewController: 0x10c768770>.
] **end** <SignUpViewController: 0x10c768770>
] **end** <SignInViewController: 0x10c769a20>
Now I am a little puzzled. What's the problem here?
Turns out transitionFromViewController:toViewController:duration:options:animations:completion: also adds the view.
This method adds the second view controller’s view to the view hierarchy and then performs the animations defined in your animations block. After the animation completes, it removes the first view controller’s view from the view hierarchy.
Which means the addSubview needs to be adjusted accordingly.
- (void) showController:(UIViewController*)newC withView:(UIView*)contentView animated:(BOOL)animated
{
UIViewController *oldC = self.childViewControllers.firstObject;
if (oldC == newC) {
return;
}
[oldC willMoveToParentViewController:nil];
[self addChildViewController:newC];
newC.view.frame = (CGRect){ 0, 0, contentView.frame.size };
if (animated && oldC != nil) {
oldC.view.alpha = 1.0f;
newC.view.alpha = 0.0f;
[self transitionFromViewController:oldC toViewController:newC duration:0.25f options:0 animations:^{
oldC.view.alpha = 0.0f;
newC.view.alpha = 1.0f;
} completion:^(BOOL finished) {
[oldC removeFromParentViewController];
[newC didMoveToParentViewController:self];
}];
} else {
[contentView addSubview:newC.view];
oldC.view.alpha = 0.0f;
newC.view.alpha = 1.0f;
[oldC removeFromParentViewController];
[newC didMoveToParentViewController:self];
}
}
old code
[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];
[self.view addSubview:toVC.view];
[toVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(0);
}];
[self transitionFromViewController:fromVC
toViewController:toVC duration:0
options:UIViewAnimationOptionTransitionNone
animations:^{
} completion:^(BOOL finished) {
[fromVC.view removeFromSuperview];
[fromVC removeFromParentViewController];
[toVC didMoveToParentViewController:self];
self.currentVC = toVC;
}];
new code, it's ok
[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];
[self transitionFromViewController:fromVC
toViewController:toVC duration:0
options:UIViewAnimationOptionTransitionNone
animations:^{
} completion:^(BOOL finished) {
[fromVC.view removeFromSuperview];
[fromVC removeFromParentViewController];
[toVC didMoveToParentViewController:self];
self.currentVC = toVC;
}];
I set the duration to 0, which means animation:NO for the transitionFromViewControllercall.

ios removeAllAnimations not working

I have an forever animation, but I want to stop it at some point I tried removeAllAnimations but it did not work.
Here is my code.
[self.backgroundImageView.layer removeAllAnimations];
-(void)animateToLeft{
if(isInCenter){
[UIView animateWithDuration:10.0f animations:^{
backgroundImageView.frame = CGRectMake(kLeftX, 0, kBackgroundWidth, kBackgroundHeight);
}completion:^(BOOL finished) {
[self animateToRight];
}];
isInCenter = NO;
}
else{
[UIView animateWithDuration:20.0f animations:^{
backgroundImageView.frame = CGRectMake(kLeftX, 0, kBackgroundWidth, kBackgroundHeight);
}completion:^(BOOL finished) {
[self animateToRight];
}];
}
}
-(void)animateToRight{
[UIView animateWithDuration:20.0f animations:^{
backgroundImageView.frame = CGRectMake(kRightX, 0, kBackgroundWidth, kBackgroundHeight);
}completion:^(BOOL finished) {
[self animateToLeft];
}];
}
This will not work in your situation.
Because [self.backgroundImageView.layer removeAllAnimations]; this will remove all layer animation which already have added by [self.backgroundImageView.layer addAnimation:/*CABasicAnimation should added here*/];
You can stop this cycle by set boolean variable in completion, then check with boolean variable.
Use a boolean value and if that is set, don't do the next animation -- also cancel pending ones..
e.g.
#interface MyClass () {
BOOL cancelAll;
}
#implementation MyClass
-(void)cancelAnimation {
self.imageView.layer removeAllAnimations]; //!
cancelAll = YES;
}
-(void)animateToLeft{
if(cancelAll)
return;
...
}
-(void)animateToRight{
if(cancelAll)
return;
...
}

Second touch animation

Trying to get to grips with Xcode and seem to be making some progress over the last few weeks.
Does anyone know a way that a custom button can do a different set of animations on a second click.
So let say I have a custom button and its of Mario, when i click it he runs from the middle of the screen and out of the right hand side of the screen and then runs back in to the middle from the left of the screen, he also makes noise.
I have achieved this using this code:
- (IBAction)marioRunning_clicked:(id)sender
{
[UIView animateWithDuration:1.50 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
marioRunning.center = CGPointMake(350.5, 456.0);
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.00 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
marioRunning.center = CGPointMake(-30.5, 456.0);
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:1.50 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
marioRunning.center = CGPointMake(160.0, 456.0);
} completion:^(BOOL finished) {
if(finished) // NSLog ( #"Finished !!!!!" );
marioRunning.center = CGPointMake(160.0, 456.0);
}];
}
}];
}
}];
marioRunning.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"mario-running2"],[UIImage imageNamed:#"mario-running3"],nil];
marioRunning.imageView.animationDuration = 0.15;
marioRunning.imageView.animationRepeatCount = 19;
[marioRunning.imageView startAnimating];
}
How could I make him do a second set of animations on the next click? For example instead of running from left to right, if i tap him the second time he will Jump up and down?
Use the selected state of the button to decide which animation to do
-(void)buttonClicked:(UIButton*)button
{
if(button.selected)
{
[self doAnimation1];
}
else
{
[self doAnimation2];
}
button.selected = !button.selected;
}
This might be overkill, but here's a cool way to do what you want:
Make a subclass of UIButton, and let's call it DDDMarioButton:
typedef void (^DDDAnimationBlock)(UIButton *button);
#interface DDDMarioButton : UIButton
- (void)addAnimationBlockToQueue:(DDDAnimationBlock)block;
#end
Then in DDDMarioButton.m:
#interface DDDMarioButton ()
#property (nonatomic, strong) NSMutableArray *animationQueue;
#end
#implementation DDDMarioButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)buttonPressed:(id)button
{
DDDAnimationBlock block = self.animationQueue[0];
block(self);
[self.animationQueue removeObjectAtIndex:0];
}
- (void)addAnimationBlockToQueue:(DDDAnimationBlock)block
{
if(!self.animationQueue)
{
self.animationQueue = [NSMutableArray new];
}
[self.animationQueue addObject:block];
}
#end
and then wherever you create your buttons, you add each step one by one:
DDDMarioButton *button = [[DDDMarioButton alloc] initWithFrame:CGRectZero];
[button addAnimationBlockToQueue:^(UIButton *button) {
// perform some animation
}];
[button addAnimationBlockToQueue:^(UIButton *button) {
// perform another animation
}];
And that should do it. I haven't tested this, you'll probably need some configuration, but that's pretty much the idea.

Resources