How to call two animations without them interfering with each other? - ios

I've made a hint box that slides up with an animation, but i also want to -10 coins when the user taps on a candle button so I created a series of photos that make an animation which depicts -10 to the user...
the problem is that every time the candle button is pressed the -10coins animation overrides the hint sliding box and replaces it, thus making it impossible for the user to find a hint again...
Code:
- (IBAction)firstHintq:(id)sender {
[_hintView setHidden:NO];
[_hintViewTwo setHidden:YES];
[_hintViewThree setHidden:YES];
_hintView.text = #"Ηταν η τεταρτη τηλεφωνικη εταιρια στην Ελλαδα";
//connected to global BOOL
if (!btn1Pressed) {
if((coins -10) >= 0){
coins = coins -10;
score = score -2;
// Load images starts
NSArray *imageNames = #[ #"coin-10-1.png", #"coin-10-2.png",
#"coin-10-3.png", #"coin-10-4.png", #"coin-10-5.png", #"coin-10-6.png",
#"coin-10-7.png", #"coin-10-8.png", #"coin-10-9.png", #"coin-10-10.png", #"coin-10-11.png",#"coin-10-12.png", #"coin-10-13.png",#"coin-10-14.png",#"coin-10-15.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 200, 30, 70)];
animationImageView.animationImages = images;
animationImageView.animationDuration = 1.5;
animationImageView.animationRepeatCount = 10;
[self.view addSubview:animationImageView];
[animationImageView startAnimating];
//coin animation ends
//changes image
[_candleone setImage:[UIImage imageNamed:#"candle2_03.png"] forState:UIControlStateNormal];
self.candletwo.hidden = NO;
//Animates the 2nd candle
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGPoint center = [_candletwo center];
center.x = 88;
center.y = 70;
[_candletwo setCenter:center];
[UIView commitAnimations];
//sets the global BOOL as true
coinsLabel.text = [NSString stringWithFormat:#"%d",coins];
btn1Pressed = true;
}
else{
[_hintView setHidden:YES];
//Show an alert that the user has not enough coins
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE" message:#"MESSAGE" delegate:nil cancelButtonTitle:#"RETURN BUTTON" otherButtonTitles:nil];
[alert show];
}
}
}
How to fix this issue, so that both animations activate properly?

For animations better use:
[UIView animateWithDuration:time // time is a double
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^() {
}
completion:^(BOOL finished) {
}];
Then you can use 2 animateWithDuration: delay: options: completion:
And OFC, you can nest 2:
[UIView animateWithDuration:time // time is a double
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^() {
}
completion:^(BOOL finished) {
[UIView animateWithDuration:time // time is a double
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^() {
}
completion:^(BOOL finished) {
}];
}];

Related

UIPickerView Animate on and off screen behavior

This is a continuation of this question
CGRectMake : How To Calculate X and Y For UIPickerView Animation?
answered by https://stackoverflow.com/users/2315974/danypata
I have a picker view that I want to animate on and off screen on a button press and using the code in the previous question/answer I have got it to animate on screen the first time the button is pressed.
the second tome the button is pressed it just disappears and then the third time it is pressed it animates to the wrong place...please help
the code
if (self.pickerSort.hidden == NO) {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.pickerSort.frame = CGRectMake(0,self.view.frame.size.height
+ self.pickerSort.frame.size.height,-self.pickerSort.frame.size.width,-self.pickerSort.frame.size.height);
}
completion:^(BOOL finished) {
}];
self.pickerSort.hidden = YES;
// [self performSelector:#selector(hidePicker) withObject:nil afterDelay:1];
} else if (self.pickerSort.hidden == YES) {
self.pickerSort.hidden = NO;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.pickerSort.frame = CGRectMake(0 ,
self.view.frame.size.height
- self.pickerSort.frame.size.height,
self.pickerSort.frame.size.width,
self.pickerSort.frame.size.height);
}
completion:^(BOOL finished) {
}];
[self.view bringSubviewToFront:self.pickerSort];
Behavior in images - button press animates onto screen beautifully
Second Press it disappears with no animation
Third Press it animates to here
Any help would be great...functionality I am looking for is how to put the picker view back on an animation so the 3rd press is the same as the first
Please try to use the below code.
self.pickerSort.hidden = NO;
NSTimeInterval duration = 0.5;
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
CGRect pickerFrame = self.pickerSort.frame;
// currently picker is off the screen, show it.
if (pickerFrame.origin.y == self.view.frame.size.height) {
// set frame to show picker
pickerFrame.origin.y = self.view.frame.size.height - self.pickerSort.frame.size.height;
} else {
// set frame to hide picker
pickerFrame.origin.y = self.view.frame.size.height;
}
self.pickerSort.frame = pickerFrame;
}
completion:^(BOOL finished) {
self.pickerSort.hidden = YES;
}];
After playing around with this and learning the ins and outs of frames and CGRects, I achieved this by using the following code
if (pickerSort.hidden == YES) {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
pickerSort.hidden = NO;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[self.tableStations addSubview:visualEffectView];
pickerSort.frame = CGRectMake(0,0,originalPickerFrame.size.width,originalPickerFrame.size.height);
visualEffectView.frame = CGRectMake(0,0,originalPickerFrame.size.width,originalPickerFrame.size.height - 64);
}
completion:^(BOOL finished) {
self.navigationController.navigationItem.leftBarButtonItem.enabled = NO;
}];
}
else
{
visualEffectView.hidden = YES;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
pickerSort.frame = CGRectMake(0,-originalPickerFrame.size.height,0,0);
}
completion:^(BOOL finished) {
self.navigationController.navigationItem.leftBarButtonItem.enabled = YES;
pickerSort.hidden = YES;
}];
}
I set the original frame size in viewDidLoad like so
pickerSort = [[UIPickerView alloc]initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
originalPickerFrame = pickerSort.frame;
pickerSort.frame = CGRectMake(0,-originalPickerFrame.size.height,0, 0);
Hope this can help somebody in the future

iOS: Changing a UIImageView by pressing an UIButton

I'm making a very simple game. I have a UIButton and an UIImageView. What I'm trying to do is that when the user presses the button, it will change the image to another image and then back to the original image, to simulate an animation (Character moving)
I have this code:
file.m
{
IBOutlet UIImageView *image1;
IBOutlet UIButton *button;
}
-(IBAction)button:(id)sender;
file.h
-(IBAction)button:(id)sender{
[UIView animateWithDuration:1.0f
animations:^{
image1.alpha = 0.1f;
} completion:^(BOOL finished) {
image1.image = [UIImage imageNamed:#"image2.png"];
[UIView animateWithDuration:0.2f
animations:^{
image1.alpha = 1.0f;
} completion:^ (BOOL finished) {
image1.image = [UIImage imageNamed:#"image1.png"];
}];
}];
}
When I open the iOS simulator and press the button, the image dissolves, then reappears and then does the animation. The problem is that I don't want it to dissolve, I just want to show the animation. How do I prevent this from happening? Is it because I'm using alpha properties?
Thank you!
I used the following code to do what I think is what you need (shown here: https://www.dropbox.com/s/vtcyw0n257tgihx/fade.mov?dl=0):
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(test)];
iv = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];
iv.image = [UIImage imageNamed:#"red"];
[self.view addSubview:iv];
}
- (void)test {
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 0.0f;
} completion:^(BOOL finished) {
iv.image = [UIImage imageNamed:#"blue"];
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 1.0f;
} completion:^ (BOOL finished) {
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 0.0f;
} completion:^ (BOOL finished) {
iv.image = [UIImage imageNamed:#"red"];
[UIView animateWithDuration:1.0f animations:^{
iv.alpha = 1.0f;
} completion:nil];
}];
}];
}];
}
You need to break it down into four parts. First you need to fade the original image out, change it to the second image, fade the second image in, fade the second image out, change back to the original image, then fade it back in.

How to make an animation then pause then continue animation?

I want to create a custom alertView that works like instagram app (login/signin view when user enters wrong password or email). The alert animation should drop down and pause for the user to read for about 2 seconds, then the alertView goes up again. How can I do this?
Here is what I have now:
- (UIView *) createAlertViewWithViewController:(UIViewController *)viewController andText:(NSString *)alertText
{
alertView = [[UIView alloc] init];
[alertView setBackgroundColor:ALERT_VIEW_COLOR];
[alertView setFrame:ALERT_VIEW_HIDE_FRAME];
UILabel *alertViewLabel = [[UILabel alloc] init];
[alertViewLabel setFrame:CGRectMake(viewController.view.bounds.origin.x, 7, viewController.view.bounds.size.width, ALERT_VIEW_HEIGHT)];
[alertViewLabel setTextAlignment:NSTextAlignmentCenter];
[alertViewLabel setTextColor:[UIColor whiteColor]];
[alertViewLabel setFont:[UIFont systemFontOfSize:13.0f]];
[alertViewLabel setText:alertText];
[alertView addSubview:alertViewLabel];
CGPoint originalCenter = alertView.center;
[UIView animateWithDuration:0.5
animations:^{
CGPoint center = alertView.center;
center.y += ALERT_VIEW_HEIGHT;
alertView.center = center;
}
completion:^(BOOL finished){
[UIView animateWithDuration:3.0
animations:^{
alertView.center = originalCenter;
}
completion:^(BOOL finished){
;
}];
}];
return alertView;
}
My code right now goes down and goes up immediately but what I want is, when it finishes the drop down animation it pauses for around 2 seconds then do the goes up animation.
I have been searching for 3 days now, but I haven't found anything one this.
Example image when it should pause for 2 seconds:
Im guessing you are looking for one of the extended versions of [UIView animateWithDuration:...
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
Pass in a delay to wait your animation.
Would you like a this library?
https://github.com/problame/CSNotificationView
Example
[CSNotificationView showInViewController:self
tintColor:[UIColor colorWithRed:0.000 green:0.6 blue:1.000 alpha:1]
image:nil
message:#"What's the meetup called?"
duration:2.f];
Try this:
UILabel *myLable=[[UILabel alloc]init];
myLable.frame=CGRectMake(0, -50, 320, 50);
myLable.backgroundColor=[UIColor grayColor];
myLable.textAlignment=NSTextAlignmentCenter;
myLable.text=#"Your Error Message";
[self.view addSubview:myLable];
[self ShowAnimation:myLable currentFrame:CGRectMake(0, -50, 320, 50) newFrame:CGRectMake(0, 0, 320, 50)];
.
-(void)ShowAnimation:(UILabel *)aLable currentFrame:(CGRect)aCurrentFrame newFrame:(CGRect)aNewFrame
{
[aLable setFrame:aCurrentFrame];
[UIView animateWithDuration:1.5 animations:^{
[aLable setFrame:aNewFrame];
}completion:^(BOOL finished)
{
[aLable setFrame:aNewFrame];
[UIView animateWithDuration:1.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn
animations:^{
[aLable setFrame:aCurrentFrame];
}
completion:^(BOOL finished) {
}];
}];
}

How to hide an item after fading it out (Objective-C)

I have the following if statement,
if (CGRectIntersectsRect(car.frame, car2.frame) && (upMovement = -1 ) && (car2.hidden == NO)){
[self bounce];
[self carexplode];
if (car2used == NO) {
addedScore = 1;
car2Used = YES;
}
car2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"car2flames.png"], nil];
[car2 setAnimationRepeatCount:0];
car2.animationDuration = 20;
[car2 startAnimating];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4];
[iceblock6 setAlpha:0];
[UIView commitAnimations];
car2.hidden = YES;
Essentially, I want it to execute the top part of the method if one car crashes into it, then using UIView animations ^^, I want it to fade out, then be hidden, so that I can set hidden to no again when it reenters the view (after it exits the screen it reloads to the top of the screen). But with this, it just automatically is hidden, without the 4 second fade out animation. How can I fix this? I've tried putting car2.hidden in an if statement, but I don't know what to put in the condition because UIView isn't really bools or integers and all that. Anyone have any advice?
[UIView animateWithDuration:1.0 delay:0.0 options:(UIViewAnimationOptions) animations:^{
//place your animation here
[iceblock6 setAlpha:0];
} completion:^(BOOL finished) {
//called when animation did finish. do what you want
car2.hidden = YES;
}];
Other way:
{
...
if (CGRectIntersectsRect(car.frame, car2.frame) && (upMovement = -1 ) && (car2.hidden == NO)){
[self bounce];
[self carexplode];
if (car2used == NO) {
addedScore = 1;
car2Used = YES;
}
car2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"car2flames.png"], nil];
[car2 setAnimationRepeatCount:0];
car2.animationDuration = 20;
[car2 startAnimating];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4.0];
[iceblock6 setAlpha:0];
[UIView commitAnimations];
[self performSelector:#selector(hideCar2) withObject:self afterDelay:4.0];
}
-(void)hideCar2
{
car2.hidden = YES;
}

iOS: why object turns to the start position after animation comletion

)) I'm trying to animate UIButton (two parallel animations: changing of images and moving) as follows, after animation button should be moved, but after completing animation button turns to the start position. Please help to fix it! Thanks in advance!
- (IBAction)bugOneTapped:(id)sender
{
[UIView animateWithDuration:1.0
animations:^{
self.bugOneButton.imageView.animationImages =
[NSArray arrayWithObjects:[UIImage imageNamed:#"bugOne3"],[UIImage imageNamed:#"bugOne2"],[UIImage imageNamed:#"bugOne1"],[UIImage imageNamed:#"bugOne2"],[UIImage imageNamed:#"bugOne3"],[UIImage imageNamed:#"bugOne4"],[UIImage imageNamed:#"bugOne5"],[UIImage imageNamed:#"bugOne4"],nil];
self.bugOneButton.imageView.animationDuration = 0.3;
[self.bugOneButton.imageView startAnimating];
self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOneButton.center.y - 50);
self.bugOnePositionY = self.bugOnePositionY - 50;
}completion:^(BOOL finished) {
[self.bugOneButton.imageView stopAnimating];
}];
self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOnePositionY);
}
Remove below line in your code
self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOnePositionY);
It will fix..
#Genie Wanted said is right,remove code
self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOnePositionY)
this will reset the button position!
remove this two line
- (IBAction)bugOneTapped:(id)sender
{
[UIView animateWithDuration:1.0
animations:^{
self.bugOneButton.imageView.animationImages =
[NSArray arrayWithObjects:[UIImage imageNamed:#"bugOne3"],[UIImage imageNamed:#"bugOne2"],[UIImage imageNamed:#"bugOne1"],[UIImage imageNamed:#"bugOne2"],[UIImage imageNamed:#"bugOne3"],[UIImage imageNamed:#"bugOne4"],[UIImage imageNamed:#"bugOne5"],[UIImage imageNamed:#"bugOne4"],nil];
self.bugOneButton.imageView.animationDuration = 0.3;
[self.bugOneButton.imageView startAnimating];
self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOneButton.center.y - 50);
}completion:^(BOOL finished) {
[self.bugOneButton.imageView stopAnimating];
}];
}

Resources