I try to remove a view from it's superview after being animated offscreen. But it seems when I add the removeFromSuperview call after when the animation is supposed to end, the view would no animate at all but instead disappear instantly from the screen.
So why is there no animation when I add the [[self pickerView] removeFromSuperview]; to the method below ?
- (void) slidePickerOutView
{
[UIView beginAnimations:#"slidePickerOutView" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.2];
[UIView setAnimationDidStopSelector:#selector(slidePickerOutViewEnded:finished:context:)];
CGRect r = CGRectMake(0, 480, 320, 244);
[[self pickerView] setFrame:r];
[UIView commitAnimations];
}
- (void) slidePickerOutViewEnded:(NSString *)id finished:(BOOL) finished context:(void *) context
{
//Stop observing the 'Done' button
[[self pickerView] removeObserver:self forKeyPath:#"valueSelectDone"];
[[self pickerView] removeObserver:self forKeyPath:#"selectedValue"];
[[self pickerView] removeFromSuperview];
[self setPickerView:nil];
}
Ok, I eventually found what was causing the animation not to start. The pickerView was being observed and whenever one if it's instance variables would change of value, the parent view would get notified and would start the animation to slide the pickerview offscreen.
But since the parentView was observing both old and new value, it got notified two times, first for the old and then for the new changed value. Thus the parentView was starting the animation two times right after each other as well, apparently this was causing the animation not to start at all.
fixed like this, just by commenting out the firstline which was sending the old value to its observer each time ValueSelectDone was changing value:
//[self willChangeValueForKey:#"valueSelectDone"];
valueSelectDone = flag;
[self didChangeValueForKey:#"valueSelectDone"];
Related
I'm writing an iPhone app and I have some UITextfields that will get covered when the keyboard appears; therefore I put the UITextFields in a UIScrollView, and set myself as the delegate so that when the the keyboard becomes active, this method gets called:
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.contentSize.width, 560);
[self.myScrollView setContentOffset:CGPointMake(0, 200) animated:YES];
}
Note, that I am making the contentSize taller so that even once the textfields have been brought into focus, the user can still scroll.
Similarly, when the textfield resigns first responder status, this method gets called:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[self.myScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.contentSize.width,self.myScrollView.frame.size.height);
}
Note, that once the keyboard has been lowered, all the content is visible, thus there is no need for scrolling to be enabled (contentSize = frame.size).
However, my problem is because I am setting the contentSize right after the contentOffset is set, the setContentOffset animation does not have time to be completed. Instead, the animation looks extremely jerky. Any suggestions?
Working with UIKeyboardDidShowNotification and UIKeyboardWillHideNotification is a good idea:
Step 1: listen to the two notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
Step 2: do something while the keyboard did show
- (void)keyboardDidShow:(NSNotification*)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
BOOL Need_Resize; // judge by yourself
if (Need_Resize) {
double offset; // judge by yourself
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[self.view setCenter:CGPointMake(self.view.center.x, self.view.center.y - offset];
[UIView commitAnimations];
}
}
Step 3: do something while the keyboard did hide
// in animation code, set the view back to the original place
[self.view setCenter:CGPointMake(self.view.center.x, self.view.frame.size.height/2)];
This solution does not need UIScrollView, just adjust the view's place, with animation, it looks great enough.
What about this?
Don't know if it will work but just from the top of my head:
[UIView animateWithDuration:0.5
animations:^{
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.contentSize.width, 560);
} completion:^(BOOL finished) {
[self.myScrollView setContentOffset:CGPointMake(0, 200) animated:YES];
}];
Good luck
Instead of setting the content offset you could try using scrollRectToVisible:animated: and pass it the rect of your textfield or CGRectZero depending on which direction you're going.
My UIViewController gets the view it is controlling to completely rebuild itself with something like this:
[self.view rebuildYourself:bIsLandscape]; // this line is in the ViewController
The view itself then contains the following method:
-(void)rebuildYourself:(BOOL)isLandscape
{
NSArray *viewsToRemove = [self subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
[self addControls]; // adds lots of views
[self layoutControlsWithOrientation:isLandscape]; // frames the views
}
I would like to animate the entire transition. I have tried this:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self.view rebuildYourself:bIsLandscape];
}
completion:^(BOOL finished){
}];
but the animation ignores the options value of UIViewAnimationOptionTransitionCurlUp and flows in from the top left corner every time.
and I have tried this:
[UIView beginAnimations:#"kkk" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView setAnimationDuration:1.0];
[self.view rebuildYourself:bIsLandscape];
[UIView commitAnimations];
but in this case the transition does curl up nicely but the underlying view is blank until the transition has finished, and then the new view suddenly 'pops' into view, which spoils the effect of the transition.
Can anyone tell me the right/best way to animate a transition where the view rebuilds itself completely?
Thanks
Make two views and put the old view overtop of the new re-built view. Then 'Curl-up' the old view so that the new view is showing and remove the old view from memory.
I want to fade the whole screen (including navigation bar) to black when a user presses a button on a uinavigationcontroler, before showing a new view. (i don't want to push this new view, for various reasons).
How would I achieve this?
EDIT
Thanks to Mac and Eiko, I have figured it out. Here's the code I used. Not sure if it is optimal, but it does the trick.
// this is called from a programmatically constructed button.
// change (void) to (IBAction) if linking from IB.
- (void)fadeAndShow:(id)sender
{
// blackView is a #property which has been #synthesize-d
// do I really need to alloc and init this?
blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
blackView.alpha = 0.0;
[blackView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar.superview addSubview:blackView];
[UIView beginAnimations:#"fadeAway" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDidStopSelector:#selector(showNewScreen:finished:context:)];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
-(void)showNewScreen:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
// I guess you could fade in somewhere in the new view controller.
// don't know how to fade back in this view tho... viewDidAppear?
NewViewController *controller = [[NewViewController alloc] initWithNibName:#"NewView" bundle:nil];
[self.navigationController setNavigationBarHidden:YES];
controller.hidesBottomBarWhenPushed = YES;
[[self navigationController] pushViewController:controller animated:NO];
[blackView removeFromSuperview];
[controller release];
}
Off the top of my head (I haven't actually tested the following at all):
-(IBAction) buttonClicked:(id) sender
{
[UIView beginAnimations:#"myAnimation" context:nil];
[UIView setAnimationDuration:ANIMATION_DURATION];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
Create a UIView in the navigationbar's superview (which I'm assuming is window-sized) that is the same size as the window.
Set that view's backgroundColor to [UIColor blackColor], and its alpha to 0.0.
In your button handler do something like the above (assuming your new UIView is blackView and ANIMATION_DURATION is your desired animation time in seconds).
Then, add your new view on top.
EDIT: too quick for me Eiko! Also, code at the top since the ordered list seems to screw around with the code formatting - sorry the answer reads a little odd.
You can add a black coloured UIView in screen size on top of your current view, and animate its alpha from 0 to 1. When the animation is done, add your new view. You can remove the black one then. Animate from 1 to 0 for the opposite effect - going from black to the content).
I'm new to iPhone development and I'm trying to do a flip animation between a UIView and another UIView containing a regular UIView and a UIScrollView, the scroll view in turn has several UIViews as subviews.
Before the animation start, the scroll view needs to be offset to a particular point to show a particular subview (the user is jumping to a particular "chapter" in the scroll view).
It is animating fine, but the problem is that it will 'sometimes' (every third time perhaps) start the animation with one of the subviews of the scroll view instead of starting the animation with the original UIView ('overlayView' in the code below). I suspect this has to do with the fact that I set the offset of the scroll view before animating.
This is how I currently do it:
// get the MPMoviePlayer window that has the views to animate between as subviews
UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
// tell the controller of the scroll view to set the scroll view offset
[instructionControlsController setInstructionImageWithNumber:[self chapterAtTime:currentTime]];
// Animate transition to instruction view
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView: moviePlayerWindow cache:NO];
// the views to animate between
[moviePlayerWindow sendSubviewToBack: overlayView];
[moviePlayerWindow bringSubviewToFront: instructionControlsController.view];
[UIView commitAnimations];
The setInstructionImageWithNumber method in the controller looks like this:
- (void) setInstructionImageWithNumber:(int)number
{
if (number < kNumberOfPages)
[scrollView setContentOffset: CGPointMake((number * kImageWidth), 0) animated:NO];
}
Any ideas of what I might be doing wrong and why I get this behavior where the animation looks fine sometimes and sometimes not?
What happens if you give the run loop a chance to update your view before beginAnimations? You might need to do this to give the view a chance to "catch up" and be accurately updated before animations start.
UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
[instructionControlsController setInstructionImageWithNumber:[self chapterAtTime:currentTime]];
//Before continuing the animation, let the views update
[self performSelector:#selector(continueTheAnimation:) withObject:nil afterDelay:0.0];
. . .
- (void)continueTheAnimation:(void*)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView: moviePlayerWindow cache:NO];
[moviePlayerWindow sendSubviewToBack: overlayView];
[moviePlayerWindow bringSubviewToFront: instructionControlsController.view];
[UIView commitAnimations];
}
I am adding some basic animation to a card game I'm working on. (My first iPhone app.)
I am creating a custom UIView class "AnimationContainer", which flips from image1 to image2, while moving from rect1 to rect2. My ultimate intention is to have up to four of these containers doing their transitions simultaneously.
The problem I'm having is that the animation isn't showing image1... so only the last half of the flip transition appears.
However, if I reset the animation first by touching Reset, then everything works perfectly. In other words, if I press Flip again and again, I only get half the transition... but if I press Reset first, then everything works perfectly for one flip.
So, how can I get the animation to reset itself correctly?
Below is the code, a screenshot, and here's a link to the complete: Project Zip File 700k.
- (void)displayWithImage1 { //RESET button calls this
self.frame = rect1;
[image2 removeFromSuperview];
[self addSubview:image1];
[self setNeedsDisplay]; //no help: doesn't force an update before animation
}
- (void)runTheAnimation { //FLIP button calls this
[self displayWithImage1]; //<---this is what the reset button calls
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:transition forView:self cache:NO];
self.frame = rect2;
[image1 removeFromSuperview];
[self addSubview:image2];
[UIView commitAnimations];
}
Thanks!
You need a drawing loop to pass in order to redraw the view before performing the animation. This code is an example of "draw this, and when the next event loop comes around, do this other thing." It's not uncommon to do this in UI code. Your first work-around is attempting the same thing, but in a much more complicated way.
- (void)_runTheAnimation {
// Moved here from -runTheAnimation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:transition forView:self cache:NO];
self.frame = rect2;
[image1 removeFromSuperview];
[self addSubview:image2];
[UIView commitAnimations];
}
- (void)runTheAnimation { //FLIP button calls this
[self displayWithImage1];
[self performSelector:#selector(_runTheAnimation) withObject:nil afterDelay:0.0];
}