I have a very peculiar problem. In an iOS app I've developed, after running the animations below I find that various other elements of the UI now animate whenever they appear.
For instance, once these animations below are run, every time I navigate to a new tab in my app, all the UI elements animate from the top left corner of the iphone to their proper positions. I have specified no animations for these UI elements in the other tabs and in fact even elements that I have no access to, like the activity indicator in the iPhone status bar, begin to animate all on their own.
What is going on? Why does this occur only after the animation below is executed? Before running this animation (with a button press) all other UI elements in other app tabs do not animate at all when they appear, which is the expected behaviour.
- (IBAction) btnChosenPostcodeClicked:(id)sender {
//prep animations
startView.alpha = 1.0;
postcodeView.alpha = 0.0;
[self.view addSubview:postcodeView];
[UIView beginAnimations:#"ChosenPostcodeAnimation01" context:nil];
[UIView setAnimationDuration:animationSpeed];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(chosenPostCodeFadeoutAnimationDone:finished:context:)];
startView.alpha = 0.0;
[UIView commitAnimations];
}
- (void)chosenPostCodeFadeoutAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
//prep animations
postcodeView.alpha = 0.0;
[UIView beginAnimations:#"ChosenPostcodeAnimation02" context:nil];
[UIView setAnimationDuration:animationSpeed];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
CGRect newRect = CGRectMake(42, 157, 239, 180);
imvBubble.frame = newRect;
postcodeView.alpha = 1.0;
}
You're missing a
[UIView commitAnimations];
in chosenPostCodeFadeoutAnimationDone so absolutely everything you do is becoming part of the animation!
Related
I am developing iOS game and need custom animation so I am using this method
CGRect basketTopFrame = mainScreenView.frame;
basketTopFrame.origin.x = 320;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
mainScreenView.frame = basketTopFrame;
[UIView commitAnimations];
in the .h file I have declared mainScreen like this
IBOutlet UIView *mainScreenView;
So in the IB I have put UIView in the view in the interface and hooked it up with mainScreenView
So in the mainViewScreen the view sometimes shows up sometimes doesn't (works on the 2nd try) however when I remove the animation code it works perfectly fine..I don't know what is happening any help would be appreciated thanks
edit
this is how I added the view
MainScreen *mainScreen = [[MainScreen alloc]initWithNibName:#"MainScreen" bundle:nil];
[mainScreenView addSubview:mainScreen.view];
I tried it in a sandbox project, and this worked for me:
- (IBAction)buttonTouched:(id)sender {
myView.transform = CGAffineTransformMakeTranslation(-320, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
myView.transform = CGAffineTransformMakeTranslation(0,0);
[UIView commitAnimations];
}
Looks like you are trying to move something off screen. An easier way is to do this
[UIView beginAnimations:#"UIBase Hide" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
mainScreenView.transform = CGAffineTransformMakeTranslation(320,0); //slide view to the right.
[UIView commitAnimations];
note: using 320 on the Translation wont move the view to the 320th pixel of the screen rather it moves your view 320px to the right. So if your mainScreenView is at origin.x = 100. After this translation it is now at 420.
To move it back do
self.transform = CGAffineTransformIdentity;
I wanted to change the animation that occurs on a navigationControllers toolbar when the method setToolbarHidden is called. By default, when you set Hidden to YES and animation to YES, the toolbar simply drops off the screen. I'd like it to slide off the screen from left to right (much like when you hit the back button on a navbar and the previous view controller reasserts itself on the screen). Is this possible?
This isn't going to be perfect but it might be possible with something like this:
self.navigationController.toolbarHidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
CGRect frame = self.navigationController.toolbar.frame;
frame.origin.x = -1*frame.size.width;
self.navigationController.toolbar.frame = frame;
[UIView commitAnimations];
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context {
self.navigationController.toolbarHidden = YES;
}
I filed a bug report and it appears, unfortunately, there isn't a way to do this until Apple adds the feature themselves.
I am working on apllication for iphone in xcode 3.1.
I want similar two images to disappear when touched one after other.
I have succeed in displaying two images on iphone simulator on touching
'PlAY' button.
Now i want that when two same images are 'touched' one after another,they should disappear.
Expect code in Objective C.
You can do this, by using simple animations the view should disappear, later you can cast the context inside the UIView animations delegate and start a second animation.
aView.alpha = 1.0;
[UIView beginAnimations:nil context:#"Context1"];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[UIView setAnimationBeginsFromCurrentState:YES];
aView.alpha = 0.0;
[UIView commitAnimations];
This id the delegate method you should implement:
-animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
I have a simple animation which simply modify the position of a button:
[UIView beginAnimation:nil context:nil];
[UIView setAnimationsDuration:3.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
mybutton.frame = CGREctMake(20, 233, 280, 46);
[UIView commitAnimations];
I want to perform some other animations when this one is finish, how to do that?
You could look at setAnimationBeginsFromCurrentState:. All the animations are run in their own threads, so [UIView commitAnimations] is a nonblocking call. So, if you begin another animation immediately after committing the first one, after appropriately setting whether or not it begins from the current state, I think you'll get the behavior you want. To wit:
[UIView beginAnimation:nil context:nil];
// set up the first animation
[UIView commitAnimations];
[UIView beginAnimation:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:NO];
// set up the second animation
[UIView commitAnimations];
Alternately, you could provide a callback by doing something like
[UIView beginAnimation:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)
// set up the first animation
[UIView commitAnimations];
//...
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
// set up the second animation here
}
a code segment for getting start.. setup initial (the first) animation:
- (void) aniPath:(AnimationPath *) path
{
[UIView beginAnimations:path->aniname context:path];
[UIView setAnimationDuration:path->interval];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(aniDone:finished:context:)];
// your initial animation
[UIView commitAnimations];
}
chains to another animation when the first is done:
- (void) aniDone:(NSString *) aniname finished:(BOOL) finished context:(void *) context
{
AnimationPath *path = (AnimationPath *) context;
if ([aniname isEqualToString:path->aniname]) {
[UIView beginAnimations:path->aniname context:context];
[UIView setAnimationDuration:path->interval];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(aniDone:finished:context:)];
// more animations, even recursively
[UIView commitAnimations];
}
}
Use the +[UIVIew setAnimationDelegate:] and +[UIView setAnimationDidStopSelector:] methods to configure your class to receive a message when the animation ends.
it quite easier.
you can just
- (void)animationDidContiune:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIView *currentObj = context;
[self callTheAnimation: currentObj];
}
you can easily loop the animation.
To end it - I would create an animationDidEnded and use it instead of animationDidContiune
once you want to stop it (like simple if in animation that chooses either to contiune or end).
HF.
AND WHAT's IMPORTANT:
use this type of animating meths:
- (void)callTheAnimation:(UIView*)itemView
{
[UIView beginAnimations:#"animation" context:itemView];
.
.
.
}
they are quite flexible - you can animate whatever here due to hierarchy of the objects.
On the Mac, the best way for a simple cross-fade transition of views (without any custom keyframe timing) is to do something such as the following excerpt:
[[self animator] replaceSubview:aView with:bView];
Unfortunately the animator property isn't available on the iPhone. What's the best bet of doing this on the iPhone? Is it setting alpha channels on each view? Sample code would be excellent.
Thanks.
The basic way to animate views on the iphone is to use the UIView beginAnimations and commitAnimations calls. They allow you to modify the animatable properties of a view and have those changes animated.
For instance I have a custom view that is hidden and shown using this approach:
- (void) showAView:(CustomAView *)aView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
aView.frame = CGRectMake(0.0f, 110.0f , aView.frame.size.width, aView.frame.size.height);
[UIView commitAnimations];
}
- (void) hideAView:(CustomAView *)aView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
aView.frame = CGRectMake(0.0f, self.view.frame.size.height, aView.frame.size.width, aView.frame.size.height);
[UIView commitAnimations];
}
By wrapping the frame property change in the UIView beginAnimations/commitAnimations the change has a standard animation applied to it.
You can add additional properties to the animation by using UIView animation class methods eg.
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];