iOS: simple animation - objective-c

I want to create a simple animation changing alpha value:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[view1 setAlpha:0.00];
[UIView commitAnimations];
Ok in this way I change the alpha value for two seconds and it works fine, but I want this thing:
When alpha is 0 the animation must should start another time to alpha = 1
So the animation should be: alpha 0 -> alpha 1 -> alpha 0 -> alpha 1 -> ... and so on with a duration of two seconds.
Can you help me?
my complete code is
-(IBAction){
FirstViewController *firstViewController = [[[FirstViewController alloc] init]autorelease];
[firstViewController.label1 setAlpha:1.00];
[firstViewController.label2 setAlpha:1.00];
view = firstViewController.viewFirst; //this is my view and I copy in other view
[self fadeOut:nil finished:nil context:nil];}
- (void) fadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fadeIn:finished:context:) ];
[view setAlpha:0.00];
[UIView commitAnimations];
}
- (void) fadeIn:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fadeOut:finished:context:) ];
[view setAlpha:1.00];
[UIView commitAnimations];
}

In iOS 4 and later you can do
view1.alpha = 1.0;
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionRepeat|UIViewAnimationAutoReverse
animations:^{
view1.alpha = 0.0;
}
completion:nil
];

- (void) fadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
if(animationRunning){
[UIView setAnimationDidStopSelector:#selector(fadeIn:finished:context:) ];
}
[view1 setAlpha:0.00];
[UIView commitAnimations];
}
- (void) fadeIn:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
if(animationRunning){
[UIView setAnimationDidStopSelector:#selector(fadeOut:finished:context:) ];
}
[view1 setAlpha:1.00];
[UIView commitAnimations];
}
These will call each other on the end of the animation...
All you need to do to start the ball rolling is call:
[self fadeOut:nil finished:nil context:nil];

You can set a animation completion handler (either using the block-based API or +(void)setAnimationDidStopSelector:(SEL)selector) and start the next one.
Alternatively have a look at those to methods:
+ setAnimationRepeatCount:
+ setAnimationRepeatAutoreverses:

I think, you can use just one function (based on previous answer's functions):
- (void) fadeIn:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(fadeIn:finished:context:) ];
[view1 setAlpha:mod(1-view1.alpha)];
[UIView commitAnimations];
}
The alplha must not be a negative value, so a mod must be applied

You create a ViewController like below
FirstViewController *firstViewController = [[[FirstViewController alloc] init]autorelease];
And then adding animation to it, but when did you add firstViewController.view in view heirarchy(addSubview). Also just calling
[[[FirstViewController alloc] init]autorelease]
wont create label object unless you have override its init method. Try debugging if label is allocated at that point of time.
Also for animation i use uiview anitmation with completion block method

Related

Comparing old and new observer values but if function is not working as expected

I've got 2 fade methods that simply change the background of a textfield. The only difference is the image used for the background. The names of the images have been copied and pasted so there's no spelling errors.
- (IBAction)fadeEnable:(UITextField *)textField;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.00];
[textField setAlpha:0];
[UIView commitAnimations];
textField.background = [UIImage imageNamed:#"background_for_text.png"];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.00];
[textField setAlpha:1];
[UIView commitAnimations];
}
- (IBAction)fadeDisable:(UITextField *)textField;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.00];
[textField setAlpha:0];
[UIView commitAnimations];
textField.background = [UIImage imageNamed:#"background_for_text_2"];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.00];
[textField setAlpha:1];
[UIView commitAnimations];
}
I have an observer on my textField which looks to see if the "enabled# property has been changed.
I'm trying to write a little bit of code that says if the enabled property changes, and it changes to enabled, then run the enabled animation. If it changes to disabled, run the disabled animation.
Unfortunately, it only runs the fadeDisabled method in both scenarios and i have no idea why.
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *) context;
{
UITextField *txtField = (UITextField *)object;
BOOL new = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
BOOL old = [[change objectForKey:NSKeyValueChangeOldKey] boolValue];
NSLog(#"new,%i",new);
NSLog(#"old,%i",old);
if ((new != old) && (new == NO))
{
[self fadeDisable:txtField];
}
else if ((new != old) && (new == YES))
{
[self fadeEnable:txtField];
}
}
I caught the mistake, for fadeDisabled, i need to change the disabled background, not the normal background.
used the following and it seems to be working fine now.
textField.disabledBackground = [UIImage imageNamed:#"background_for_text_2"];

setAnimationDidStopSelector: not getting called

I know this question gets asked a lot but i have tried all the other answers and i can't figure out why the animation stop selector does not get called. Here is the code:
-(void) moveImage:(UIImageView *)image duration:(NSTimeInterval)duration curve:(int)curve x:(CGFloat)x y:(CGFloat)y annKey:(NSString *) annKey{
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
image.transform = transform;
[UIView beginAnimations:annKey context:NULL];
[UIView commitAnimations];
}
This is the main animation function that gets sent all the correct parameters.
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
if ([animationID isEqualToString:#"Burn"])
{
NSLog(#"Animation: %# has finished.",animationID);
}
NSLog(#"This does not get called, why not?");
}
None of my NSLogs display text. What am i doing wrong?
You need tho do -
[UIView beginAnimations:annKey context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
// Now define rest.
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
image.transform = transform;
[UIView commitAnimations];
Not sure what the problem is, but you really shouldn't be using beginAnimations/commitAnimations. Instead, use block animations. You define the completion code directly in the completion portion of the call.
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
// animation code
}
completion:^(BOOL finished){
// completion code
}];

fade in, fade out animation to uilabel

i have a label that i want to fade in and then to fade out.
here is my code:
-(void) fadein
{
scoreLabel.alpha = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2];
scoreLabel.alpha = 1;
[UIView commitAnimations];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
scoreLabel.alpha = 0;
[UIView commitAnimations];
}
from this code i get this situation: my label is fade in and then i don't see the fadeout animation.
how can i fix it?
-(void) fadein
{
scoreLabel.alpha = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//don't forget to add delegate.....
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2];
scoreLabel.alpha = 1;
//also call this before commit animations......
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
scoreLabel.alpha = 0;
[UIView commitAnimations];
}
The call to setAnimationDidStopSelector should be before commit the animations:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
scoreLabel.alpha = 1;
[UIView commitAnimations];

how to animate a UIView same as the working of presentModalViewController ( ie [self presentModalViewController:child animated:YES];)

I am new in ios development, Now i am doing some animation on my application.In my application has one menu on the bottom of main view and have two button one for hide the menu and other for show the menu .My needs is the show and hide function of menu is working like
the [self presentModalViewController:menuView animated:YES]; and [self dismissModalViewControllerAnimated:YES]; function(ie. click the show button ,pop the menuView from the bottom of main view and click the hide button ,move down the menuView ) . I know the basic animation like:
[UIView beginAnimations:#"ShowHideView" context:nil];
[UIView setAnimationCurve:UIViewAnimationOptionOverrideInheritedCurve];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[menuView setAlpha:0];
[UIView commitAnimations];
If any body know ,please help me.
When you tap showMenuView, do as following,
- (IBAction)showView:(id)sender
{
[self.view addSubview: menuView];
CGRect rect = menuView.frame;
rect.origin.y = 480;
menuView.frame = rect;
[UIView beginAnimations:#"ShowView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
rect.origin.y = 0;
menuView.frame = rect;
[UIView commitAnimations];
}
And to hide,
- (IBAction)hideView:(id)sender
{
CGRect rect = menuView.frame;
[UIView beginAnimations:#"HideView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
rect.origin.y = 480;
menuView.frame = rect;
[UIView commitAnimations];
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[menuView removeFromSuperview];
}

How to remove previous ViewController?

Seeking help.
My Project:
i make a book for children.
Every page is a custom ViewController.
On every Page i have a button for next and previous Page.
When the NextPage-Button is pressed I "switch"/add a ViewController in the AppDelegate like this:
- (void)goToNextPage2 {
self.view2 = [[ViewController2 alloc] init];
view2.view.frame = CGRectMake(769, 0, 768, 1024);
[window addSubview:view2.view];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
view2.view.frame = CGRectMake(0, 0, 768, 1024);
[UIView commitAnimations];
}
How and where do i remove the previous Viewcontroller?
In this case it would be ViewController1.
Any idea?
Thanks in advance
Planky
You didn't provide much information...
Anyway, that might help.
In your animation code:
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
And add that to remove the ViewController:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[self.view1.view removeFromSuperview];
self.view1 = nil;
}