Steady Animation? - objective-c

I am making a iPhone/iPad app with a cursor and music. I am running a timer every beat and moving it like so:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:theLength];
[UIView setAnimationBeginsFromCurrentState:YES];
theCursor.frame = CGRectMake(theX, 0, 6, 320);
[UIView commitAnimations];
However, it seems to start off fast then slow down as it gets to the end. Is there any way to make the animation steady?

That's the animation curve. By default it is set to UIViewAnimationCurveEaseInOut. Use setAnimationCurve: to set it to UIViewAnimationCurveLinear.
You should also be aware that that way of animating is deprecated and Apple now recommend using the block-based animation style.

Related

How to flip two views Objective C iOS 13

I have two views that flip (like Turing over a card horizontally). iOS 13 has depreciated the begin animations code and I'm trying to figure out how to use [UIView animateWithDuration:delay:options:animations:completion:]
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.ABCard cache:YES];
[self.ABCard exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
I'm trying to do the following but the animation just switches views instantly with no flip action.
[UIView animateWithDuration:1.0 delay:1.0
options: UIViewAnimationCurveEaseInOut | UIViewAnimationTransitionFlipFromLeft
animations:^{
[self.ABCard exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
} completion:^(BOOL finished){
}];
exchangeSubviewAtIndex doesn't change the look of the animation so it's won't result in an animation, which is why you just see it flip.
Results from this answer may help you with a proper solution: iPhone Flip Animation using UIViewAnimationTransitionFlipFromLeft in landscape mode

Stop user interaction on view

My code does an animation that flips from right to left. The problem is while its switching, only a .75 second animation, the user is still able to interact with the program. I dont want them to be able to, is there a way to stop all user interaction for a short time, or just a way to stop it completely, then i can just use a timer to put it back on. here is my code for the animation:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:[self view]
cache:YES];
[UIView commitAnimations];
Thanks, Jacob
Try:
[self.view setUserInteractionEnabled:NO];

How to stop executing FOR loop until code is completed

I made this thing. It has no purpose other than for demonstration to myself. Essentially, I'm moving a view that I created around the screen in a loop. The problem is that it doesn't wait until it's done with one animation before starting the other, and so the view bypasses the first two and ends up at the last toastRect after the for loop is done, and it doesn't keep moving after that. How can I force the code to complete each animation before moving on to the next?
int i;
for (i=0; i < 100; i++) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:9];
toast.view.frame = toastRect1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:9];
toast.view.frame = toastRect2;
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:9];
toast.view.frame = toastRect;
[UIView commitAnimations];
NSLog(#"%d",i);
}
}
I think you need to use + (void)setAnimationDidStopSelector:(SEL)selector. Take a closer look at http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html if you want.
You can use the setAnimationDidStopSelector method to chain the animations. Check the docs for details: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html%23//apple_ref/occ/clm/UIView/setAnimationDidStopSelector:

How to let two animations for UIView executed one by one

I have a small UIView and want to let it move to center of screen first, and then, zoom to full screen.
But when I begin start an animation like
[UIView beginAnimations:#"animation1" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(showDetailToFullscreen)];
self.currentDetailVC.frame = centerFrame;
[UIView commitAnimations];
And in the same controller, I have a method:showDetailToFullscreen
- (void)showDetailToFullscreen {
CGRect screenFrame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView beginAnimations:#"animation2" context:nil];
[UIView setAnimationDuration:0.5];
self.currentDetailVC.view.frame = screenFrame;
[UIView commitAnimations];
}
but when start, it still executed together.
I think the problem they are still in the same transaction. But how can I let these two animation executed one by one? Appreciate for any answer!
Try this instead of the setAnimationDidStopSelector:
[self performSelector:#selector(showDetailToFullscreen) withObject:nil afterDelay:0.5];
It's now considered much better to annimate views using the 'blocks' methods that can be found in the "Animating Views with Blocks" section in the UIView Class Reference.
In most of these methods you can specify a "completion" block of code that can be used to start off a second animation when the first animation completes. You could also call showDetailToFullscreen within this completion block.
I'd reccommend trying this method instead.
Are you targetting iOS 4 or later? If so, I recommend using the block-based animation methods instead. What you want to do is very easy:
Example from UIView docs:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];

Cocoa Touch - Slow Moving CGPointMake

I'm using the following code to move an UIImageView across screen. With this code it rockets across the screen but I want it to slowly move. What do I need to do?
[UIView beginAnimations:#"slide-up" context:NULL];
astroid_1.center = CGPointMake(astroidPosX,astroidPosY); // change this to somewhere else you want.
[UIView commitAnimations];
Thanks!
You need to set the duration of the animation:
[UIView setAnimationDuration:1.0];