Chaining UIView animations? - ios

I am trying to make an image start at coordinates x=80 and y=249 which would move to x=284 and y=99. Then from there it would go to x=488 and y=249.
This is what i got, but for some reason the image starts at x=284 and y=99 and moves to x=488 and y=249.
I need help to fix this issue.
-(void)BallArchR{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 1.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
CGPoint center = [Ball center];
center.x = 284;
center.y = 99;
[Ball setCenter:center];
[UIView commitAnimations];
[self BallArchR2];
}
-(void)BallArchR2{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 1.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
CGPoint center = [Ball center];
center.x = 488;
center.y = 249;
[Ball setCenter:center];
[UIView commitAnimations];
}

because of you call BallArchR2 method from BallArchR method directly. But in actual, the animation of BallArchR method is not complete and before that BallArchR2 method is called. So, call BallArchR2 method after the completion of first BallArchR method animation. So you can Try this hope it helps you:
-(void)BallArchR{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 1.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
CGPoint center = [Ball center];
center.x = 284;
center.y = 99;
[Ball setCenter:center];
[UIView commitAnimations];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1.55 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self BallArchR2];
});
}
-(void)BallArchR2{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 1.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
CGPoint center = [Ball center];
center.x = 488;
center.y = 249;
[Ball setCenter:center];
[UIView commitAnimations];
}

Related

About iOS10 animation

I'm coding a login view, when the keyboard show, background will move the same distance with an animation, but I find a question that I can't understand, when the animation begin, at the top of the background will display a white area, the white are is just the area that will disappear when the animation finished.
Here is my code
- (void)keyboardWillShow:(NSNotification *)notification{
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
NSNumber * duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber * curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = 0 - keyboardBounds.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
[UIView setAnimationDelegate:self];
self.bgView.frame = viewFrame;
[UIView commitAnimations];
}
Thanks in advance!

How to use setAnimationStartDate in the UIView?

How to use setAnimationStartDate?
The following code does not work.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:3];
[UIView setAnimationStartDate:date];
CGPoint point = _imageView.center;
point.y += 150;
[_imageView setCenter:point];
[UIView commitAnimations];
You could use block-based animations. They are much cleaner and can do exactly what you need. One method to look at is +[UIView animateWithDuration:delay:options:animations: completion:]. The delay parameter would be useful in your situation. Here is an example.
[UIView animateWithDuration:10
delay:3
options:UIViewAnimationOptionCurveEaseIn
animations: ^{
CGPoint point = _imageView.center;
point.y += 150;
[_imageView setCenter:point];
}
completion: ^(BOOL finished) {
NSLog(#"Animation Complete");
}];
You may have to mess around with the delay to mimic your NSDate setup.

Resize an image but never came back to original size

I'm trying to resize an image (tiny and return to original size). This code is working for resize the image but never go back to the original size. I thin I miss something in the endItem1Animation code. Any suggestions? Thanks
if (CGRectIntersectsRect(image.frame, item1.frame)) {
item1.hidden = YES;
// Item 1 Animation (Go Tiny)
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];
CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
[UIView commitAnimations];
[self endItem1Animation];
This is the endItem1Animation code:
// Item 1 End Animation (Return to Original Size)
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:5.0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];
CGRect newRect = image.frame;
newRect.size = CGSizeMake(50,50);
image.frame = newRect;
[UIView commitAnimations];
The problem is that when you call beginAnimations / endAnimation, this does not block the main thread from running, so calling [self endItem1Animation] sets the second animation which will prevent the first animation from running.
You'll want to use the UIView animation block method instead, which lets you supply a completion handler when the animation completes.
[UIView animateWithDuration:0.5 animations:^{
CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
} completion:^(BOOL finished) {
[self endItem1Animation];
}];
Another, more straightforward issue, is that the duration for the first animation is zero.
I fix this issue running a selector at the end of the code. Now my code is working. Here is my final code, I Hope to help someone. Thank you for your suggestions.
if (CGRectIntersectsRect(image.frame, item1.frame)) {
item1.hidden = YES;
// Item 1 Animation (Go Tiny)
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];
CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
[UIView commitAnimations];
[self performSelector:#selector(endItem1Animation) withObject:nil afterDelay:8.0];

how to block an action if user does not have enough coins?

I'm fixing a word game and I've made a button that allows a user to get a tip... but the tip cost 10 coins.
how can i block the action if the user doesn't have any coins left and at the same time Alert the user to buy coins?
-(IBAction)btnHint:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGPoint center = [hints center];
center.x = 160;
center.y = 230;
[hints setCenter:center];
[UIView commitAnimations];
hintView.text = #"founded in 1996, and is a sub of telecome";
coins = coins -10;
}
Something like this:
- (IBAction)btnHint:(id)sender {
if (/* some condition that determines if there are enough coins */) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGPoint center = [hints center];
center.x = 160;
center.y = 230;
[hints setCenter:center];
[UIView commitAnimations];
hintView.text = #"founded in 1996, and is a sub of telecome";
coins = coins -10;
} else {
// show alert
}
}
BTW - you really should migrate to the modern block-based UIView animation instead of the old way you are using.

how to make a button do two actions - elevate up and when pressed the second time go down?

Is their a new way to write this code... Anyway, I need the user to press the button to first go down and then go up the down measurements are 229 for y and 160 for x... how is this done?
- (IBAction)closeHints:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGPoint center = [hints center];
center.x = 160;
center.y = 290;
[hints setCenter:center];
[UIView commitAnimations];
}
- (IBAction)closeHints:(id)sender {
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
CGPoint center = [hints center];
if(hints.center.y != 290){
center.y = 290;
}
else{
center.y = originY; //you should define it
}
[hints setCenter:center];
}
completion:nil];
}

Resources