Simple UIView animation move doesn't work - objective-c

I have no idea, why it is not working.
All I want to do is a simple UIView animation in the viewDidLoad.
Here's my code:
[UIView animateWithDuration:3.0f animations:^{
[self.headline setCenter:CGPointMake(0.0, 200.0)];
}];
Nothing happens. When I test the general approach of calling a animation method on that particular object like this:
[UIView animateWithDuration:3.0f animations:^{
[self.headline setAlpha:0.0];
}];
it works!!! Why am I not able to move the view across the screen? I am using latest Xcode 4.5.
Thanks for any advice!
UPDATE:
When I add a view manually in code it works. But for the UIViews I create as Outlets in the Interface Builder it doesn't work.
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 100.0)];
testLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:testLabel];
[UIView animateWithDuration:3.0 animations:^{
testLabel.center = CGPointMake(0.0, 200);
}];
So obviously I am doing something wrong in the .xib file

Dont do it in viewDidLoad. The view is not pressent at that time yet.
Try it in viewDidAppear.

Well, I think the reason for the missing animation was the fact that I called a different push navigation animation from the view controller that was pushing the actual view on screen:
- (IBAction)goForSelection:(id)sender
{
SelectionViewController *selectionViewController = [[SelectionViewController alloc] initWithNibName:#"SelectionViewController" bundle:nil];
//[self.navigationController pushViewController:selectionViewController animated:YES];
[UIView transitionWithView:self.navigationController.view duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
[self.navigationController pushViewController:selectionViewController animated:NO];
} completion:^(BOOL finished) {
[selectionViewController startIntroAnimation];
}];
}
First I checked what happens when I use the default navigation controller segue. And to my surprise the animation did start. Then I inserted the call to the [selectionViewController startIntroAnimation] to the completion block and this works as well.

Related

Strange controls animation after transitionWithView

I am trying to transition from one view to another.
The final view I'm transitioning to is a XIB with a couple buttons and a title bar.
My problem is that after the transition, the buttons get displayed on the very top left of the window for a fraction of a second, and eventually "drag" to their expected coordinates. Everything is perfectly usable and functional, but the transition looks a little funny.
Any idea what could cause this?
Here is my code snipet:
Homepage *hp = [[Homepage alloc] init];
hp.data = self.data;
[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionCurlDown
animations:^ { [self.view addSubview:hp.view]; }
completion:nil];
[hp DisplayThumbnails];
[hp release];
I wanted to provide the code snippet that I've used to resolve this problem using Jacob's recommendation in case someone else runs into this. Here it is:
Homepage *hp = [[Homepage alloc] init];
hp.data = self.data;
[hp.view setHidden:YES];
[self.view addSubview:hp.view];
[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionCurlDown
animations:^ { [hp.view setHidden:NO];}
completion:nil];
[hp DisplayThumbnails];

PresentModalViewController with the same animation as push view in a navigation controller?

Is it possible to use the PresentModalViewController with the same animation that is shown when using the "pushViewController" method of a navigation controller?
As far as I know, it can't move horizontally. And what's more, when you use presentModalViewController:animated:, the previous view will disappear(be blank). But you can do like this(add your desired view to the top of your window):
UIViewController *modalViewController = [[UIViewController alloc] init];
[modalViewController.view setFrame:CGRectMake(320.0f, 0.0f, 320.0f, 480.0f)];
[modalViewController.view setBackgroundColor:[UIColor redColor]];
[[UIApplication sharedApplication].delegate.window addSubview:modalViewController.view];
[UIView animateWithDuration:0.25f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[modalViewController.view setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
}
completion:nil];
[modalViewController release];
I would recommend UIStoryboardSegue instead of the approved answer above. It would seem that it's the proper way to achieve the effect and view hierarchy.
It can be used in conjunction with presentModalViewController:animated: as noted at the end of the documentation under "perform".
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIStoryboardSegue_Class/Reference/Reference.html

presentModalViewController translucent with previous view in the background

Im trying to present a view translucently and that the previous view sticks around and be that its visible in the background.
I've got
[self presentModalViewController:modalView animation:YES];
and I have the transparency set in the modalView's viewDidLoad, but after modalView gets brought up the previous view disappears. What can I do to keep the other view to stay around in the background?
I have also tried adding it with
[self.view addSubview:modalView.view];
It doesn't cover the whole screen, I would like to be able to solve this problem using presentModalViewController method.
It sounds like you just want a view to be displayed on top of your main view. Modal views are a finicky way of presenting subviews, instead you should look at creating a simple view class to add to your view controller. You can then use [UIView animate...]; method to animate it in and out of view.
To get you started:
- (void)displayViewButtonPressed(id)sender
{
if (!self.topView)
{
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(44.0f, 22.0f, 40.0f, 44.0f];
[overlayView setAlpha:0.0f];
[overlayView setBackgroundColor:[UIColor redColor]];
[self setTopView:overlayView];
[overlayView release];
}
[self.view addSubView:self.topView];
[UIView animateWithDuration:0.5
animations:^{
[self.topView setAlpha:1.0f];
}];
}
In the above method we create a custom UIView and animate it into position. We maintain a pointer to it so we can remove it later (like so:)
- (void)dismissViewButtonTapped:(id)sender
{
[UIView animateWithDuration:0.5
animations:^{
[self.topview setAlpha:0.0f];
}
completion:^(BOOL finished) {
[self.topView removeFromSuperView];
}
}
Its a little more work that using modal views, but it gives you much more flexibility in regards to what you use and how you display it.
Hope this helps:)

remove every type of subview from uiscrollview?

hello all i am using this code to show flip animation...... i have a uiview with scrollview(paging enabled)...so it shows a view like a page...now i also have done flip animation using this code....
-(void)flipView
{
flashCardAnswerController *flashCardAnswerControllerobj = [[flashCardAnswerController alloc] initWithNibName:#"FlashCardAnswerView" bundle:[NSBundle mainBundle]];
[flashCardAnswerControllerobj.view setFrame:[[self view] frame]];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.60];// Sub. duration
UIView *superview;
if ((superview = [[self view] superview])) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:superview cache:YES];
[superview addSubview:[flashCardAnswerControllerobj view]];
}
[UIView commitAnimations];
}
so according to code the new view will be added to scrollview..i also manage to release the new view while flipping back...but if i directly push the back button on navigaion controller...the flip side view won't release. i tried this in dealloc method of my scrolleview's class but it doesn't work
for(UIView *subview in [scrollView subviews]) {
[subview removeFromSuperview];
}
how do i remove flipside's view the views are very much in number...approx. 48 plus 48 flip side views so definietly it will create memory issues on device...
also could someone tell me a way to disable NavigationBar's back button in flip side view...without creating scolling class's object.
I do not have a direct answer to your question, but I can tell that this is the old iPhone OS 2.x way of displaying a views flip-side.
In iPhone OS 3.0 you should use presentModalViewController:animated: instead. This is much less code, and all of the problems you describe in your questions goes away.
The idea is that the flip-side of the current view, is managed by a separate UIViewController that you display modally. But in addition to the sliding animation from 2.0 you can also have the new view controller animate in and out with a horizontal flip.
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:flipSideController
animated:YES];
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)]

getting a view controller to disappear

I'm trying out a multiple view application, but I can't seem to get the first view controller to go away when I bring in the new view controller. I'm laying the second (coming) view controller at index 0, and it's just placing it in the background. I thought the [going.view removeFromSuperview] would remove the original viewcontroller, but that's not what is happening...
UIViewController *coming = nil;
UIViewController *going = nil;
UIViewAnimationTransition transition;
if (answer == YES)
{
coming = boyController;
going = getInfoController;
transition = UIViewAnimationTransitionFlipFromLeft;
}
else
{
coming = girlController;
going = getInfoController;
transition = UIViewAnimationTransitionFlipFromLeft;
}
NSLog(child);
[UIView setAnimationTransition:transition forView: self.view cache:YES];
[coming viewWillAppear:YES];
[going viewWillDisappear:YES];
[going.view removeFromSuperview];
[self.view insertSubview:coming.view atIndex:0];
[going viewDidDisappear:YES];
[coming viewDidAppear:YES];
[UIView commitAnimations];
First a little refactoring:
coming = (answer ? boyController : girlController);
You can delete going and transition, as they're only used once. Then, to actually do the animation, you need to put everything in the context of an animation block.
[UIView beginAnimations:#"flipAnimation" context:NULL];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[getInfoController.view removeFromSuperview];
[self.view addSubview:coming.view];
[UIView commitAnimations];
viewWillAppear: and viewWillDisappear: are delegate methods. These will be called automatically on those views' delegates, if any. They shouldn't ever be called manually.