making an object move without cocos2d? - iphone

I am trying to make a button move in my application without having to implement the cocos2d framework in my application. I am making a data application and dont want to add any other game elements except for a button that moves. Does anyone know how to do this?

To move any object you simply have to give it a new frame by setting
myView.frame = CGRectMake( x, y, width, height );
You can even easily have the phone animate it for you without any added frameworks by wrapping it with [UIView beginAnimation...] blocks.
Here's an example:
[UIView beginAnimations:#"moveButton" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5];
someButton.frame = CGRectMake( newX, newY, newWidth, newHeight );
[UIView commitAnimations];

You can move a view directly using
[theview setFrame:CGRectMake(x,y,w,h)];
or you can use animations to do so a in cooler way (this should be the iOS 4.x Way)
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionLayoutSubviews
animations:^{ [theview setFrame:CGRectMake(x,y,w,h)]; }
completion:^(BOOL finished){
[self iAmDoneAnimatingNowICanDoSomethingElse]; }];

Related

How can I animate zoom in / zoom out on iOS using Objective C?

I'm looking to replicate the zoom in / zoom out animations so common in iOS applications (example #1, #2). I am specifically looking for a source that can provide some common library with pre-specified values for ideal sort of animations. Like for zoom in, it should come with preconfigured transform values that are easily identifiable by human eye. Something like pop-animation and so on.
I assume these must be fairly well supported in iOS, either by a library or direct API support... But I'm not sure where to even start.
Use following code for zoom in and zoom out animation.
For Zoom In:
- (void)popUpZoomIn{
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[UIView animateWithDuration:0.5
animations:^{
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
} completion:^(BOOL finished) {
}];
}
For Zoom Out:
- (void)popZoomOut{
[UIView animateWithDuration:0.5
animations:^{
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
} completion:^(BOOL finished) {
popUpView.hidden = TRUE;
}];
}
Animations like that can be done without the need for 3rd party libraries.
Example:
self.frame = CGRectMake(0.0f, 0.0f, 200.0f, 150.0f);
[UIView beginAnimations:#"Zoom" context:NULL];
[UIView setAnimationDuration:0.5];
self.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
[UIView commitAnimations];
Example Using Scale Also
UIButton *results = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 100)];
[results addTarget:self action:#selector(validateUserInputs) forControlEvents:UIControlEventTouchDragInside];
[self.view addSubview:results];
results.alpha = 0.0f;
results.backgroundColor = [UIColor blueColor];
results.transform = CGAffineTransformMakeScale(0.1,0.1);
[UIView beginAnimations:#"fadeInNewView" context:NULL];
[UIView setAnimationDuration:1.0];
results.transform = CGAffineTransformMakeScale(1,1);
results.alpha = 1.0f;
[UIView commitAnimations];
source: http://madebymany.com/blog/simple-animations-on-ios
Applied on xCode 7 and iOS 9
//for zoom in
[UIView animateWithDuration:0.5f animations:^{
self.sendButton.transform = CGAffineTransformMakeScale(1.5, 1.5);
} completion:^(BOOL finished){
}];
// for zoom out
[UIView animateWithDuration:0.5f animations:^{
self.sendButton.transform = CGAffineTransformMakeScale(1, 1);
}completion:^(BOOL finished){}];
What I was looking for, considering the examples I gave, was a layer of abstraction that would give me most commonly used animation types.
Such types would enable the developer not only to include common animations like zoom in / zoom out, but also would incorporate optimum animation values (To, From, Timing etc.) so that developer doesn't have to worry about those.
I found one such library here, and I believe there are many more.

Animating self.view.window.frame change

I'm using animation with blocks and I'm trying to animate a change to my main window's frame.
Here is my code:
[UIView animateWithDuration:0.3f animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.view.window.frame = CGRectMake(0.0f, -20.0f, self.view.window.frame.size.width, self.view.window.frame.size.height+20);
}];
However it doesn't animate like self.view.frame changing or something else. It's just plain crap without animation.
Any ideas?
This is Wrong, Window is Fixed in FRAME And Bounds,
you cannot move Window, its top Root Container and has constant size, you should translate and transform your view only like this.
[UIView animateWithDuration:0.3f animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.view.frame = CGRectMake(0.0f, -20.0f, self.view.frame.size.width, self.view.frame.size.height+20);
}];

How to implement UIViewAnimationOptionBeginFromCurrentState

I have a piece of audio & associated Play & Stop buttons, when the Play button is pressed I use an animation of a curser to denote at which point in the audio sample we are, at a given moment. Whenever the stop button is pressed I want my curser to return to its initial coordinates. I believe UIViewAnimationOptionBeginFromCurrentState might be the way to do this? My code for the initial animation is below, anyone have any tips on how to use UIViewAnimationOptionBeginFromCurrentState in order to send the cursor back to its original coordinates?
Thanks for any help in advance :)
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2.0f];
yellowBar.frame = CGRectMake(310, 20, 5, 100);
[UIView commitAnimations];
If you are still using beginAnimations/commitAnimations, use setAnimationBeginsFromCurrentState:. Otherwise, you can pass UIViewAnimationOptionBeginFromCurrentState to animateWithDuration:delay:options:animations:completion: for the options parameter.
Brian is right, you have got two alternatives:
//Initialize newFrame here
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState //Multiple options
animations:^ {
[yourView setFrame: newFrame];
}
completion:^ (BOOL finished) {
}];
Or
//Initialize newFrame here
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
[yourView setFrame: newFrame];
[UIView commitAnimations];
AFAIK you can only use that option with block-based animations (which you are encouraged to use anyway). You'd use the animateWithDuration:delay:options:animations:completion: method, described here.

Animate two views like when you pass a phone call

I have made many tries to have the same effect of switch than the one you can see when calling a phone number : the front view disapear with a zoom effect, while the other one appears also with a zoom effect.
I can't achieve doing that effect, it's always a so-so. There seems to have a scale effect, with an action on opacity, but...
Do you know how this can be done to reflect that effect (not a so-so, I have tons of those) ?
// Make this interesting.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(startupAnimationDone:finished:context:)];
self.view.alpha = 0.0;
self.view.frame = CGRectMake(self.view.center.x,self.view.center.y , 0, 0);
[UIView commitAnimations];
}
I hope this is a framework for a better animation. It's not guaranteed to work, but it's the best I could think of for the first part of the transition.
This will change the frame of your view controller's view so that it "sucks" itself into the center of the screen, which is a natural black color. Now that I think about it, a CGAffineTransform would have been much easier...
EDIT (For pedantry's sake): Starting with iOS 4, you can now use the newer, and much more syntactically clean, animation blocks and a nice transform:
[UIView animateWithDuration:0.25 delay:0.0 options:0 animations:^{
//animate the toolbars in from the top and bottom
[myTopView setFrame:CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44)];
[myBottomView setFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
//fade to black...
self.view.alpha = 0.0;
//and suck inwards.
self.view.frame = CGRectMake(self.view.center.x,self.view.center.y , 0, 0);
}
completion:^{
//do something to end nicely, or start another animation block.
}];

How to animate a NIB so that it enters the screen from the right side

I guess this is quite basic, but I was wondering if there is an easy way to animate an entire NIB onto the screen... coming from the right. Ideally, I'd like it to bounce a bit once it stops (i.e. if the NIB is supposed to scroll to position X = 0, I'd like to go it slightly negative before coming back to 0).
This is what I have so far:
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
TabsEdit *tEdit = [[TabsEdit alloc] initWithNibName:#"TabsEdit" bundle:nil];
self.tabsEdit = tEdit;
[self.view addSubview:tEdit.view];
[tEdit release];
}
completion:^(BOOL finished){
}
Is there any easy solution or will I need to animate every single element in the NIB into view and then add my little bounce effect to it?
I'd be grateful for any suggestions of how to achieve this efficiently.
To get more complicated animations (if you want to have more segments to the bouce), you should go down to the CoreAnimation layer and define keyframes. Else have a series of embedded animateWithDuration/Completions, with each one doing a different part of the bounce). If all elements of a view are subviews, then when the view is animated, its subviews will follow.
one move repeat to bounce.
[UIView beginAnimations:#"move" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
if(movingIn){
view.frame = CGRectMake(0.0f, view.frame.origin.x, .view.frame.size.width, view.frame.size.height);
}else{
view.frame = CGRectMake(320.0f, view.frame.origin.x, .view.frame.size.width, view.frame.size.height);
}
[UIView commitAnimations];