UIView Animation: Shrink - iphone

I'm looking to have my main view shrink to reveal the next view in the same way the Facebook app's views shrink when you press the top-left button. I already have it working with one of the included animations like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[self.navigationController popToRootViewControllerAnimated:YES];
[UIView commitAnimations];
However, since "Shrink" isn't one of the included animations, I'm a bit stuck. How could I make this shrink instead?
I'm fairly well-experienced with the iPhone SDK but haven't spent a lot of time with UIView animations.

Have you tried combining a scaling and translating transformation?

UIView's setAnimationTransition: method makes the packaged set of animations dirt simple, but if you want to do something else, you have to drop down a level and use Core Animation itself.
It isn't too bad: basically you use CATransaction's begin and commit methods and in between, get the view's layer and set its transform property directly. To shrink it you could set the scale to 0.00001, causing it to shrink.
Instead of removing the view right away, you will have to set a completion block and remove it yourself when the animation is done. And you might want to reset the transform back to normal, if you plan to use the view again.

Related

How can I animate my views with the NSLayoutConstrains (auto layout) workaround?

I'm working to get my apps configured for the new iPhone 5. So I started with investigating the auto-layout guide of cocao touch ([link][1]) and the WDC masterclasses.
But still one thing is unclear to me. How can I animate my views.
In my previous Apps I used the regular UIView animation like:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
MyView.frame = CGRectMake(0,100, 100, 100);
[UIView commitAnimations];
Or the CGAffineTransform methods.
Can someone tell me what the best workaround is when working with auto-layout, since I'm unable to refer to frames and don't make frame-declarations anymore? Settings timers and removing and adding constraints?
The constraints are really not at all related to the animations.
However, you should use the up to date block based animation methods such as animateWithDuration:animations:.
You should use
[MyView layoutIfNeeded];
in animation block.
Yeah, when you use Auto-layout all the setFrame methods are removed. In your case if you want the View to move about certain number of pixels you can try moving the center of the view instead of setting the frame of the view and set the priority of the constraints around the view to low.

Smooth out resizing table cells during reload on rotation

I have a project that includes a table that is dynamically created and formatted without using IB. The issue is, that the table needs to change both values and size during a rotation. However, the resizing is choppy and occurs suddenly during the animation. Currently I'm using the didRotate method, and in that method I'm calling the reload method in order to resize it. Should I be using a different method to do this, or is there anyway to at least smooth out the resizing?
Edit: I'm trying to use the replacement/resizing within the willRotateToInterfaceOrientation:, or possibly the two step animation. However, these methods are never called, is this due to the view hierarchy (the view I'm trying to resize is part of a split view)?
Do you tell the didRotate method to use animation when making the changes? For example, here is a code snippet that makes a flip transition. The key is to use the "beginAnimations" and "commitAnimations" to tell the iPhone how to handle those changes. The code in between describes the changes. When the "commitAnimations" is called, the changes are magically animated.
[UIView beginAnimations:#"leftPortrait" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:leftView cache:YES];
[leftWebView setFrame:CGRectMake(0, 0, 384, 916)];
[UIView commitAnimations];
Hope this helps.

Animating UIView problem (matching the expanding animation to the shrinking animation)

I'm trying to animate a UIView so that it can grow larger when tapping a button and shrinks down to its original size by tapping the button again.
I do this by adjusting frame to its new size and origin and running the code below.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.theView.frame = frame;
[UIView commitAnimations];
When animating to a bigger size the animation is how it should be. But when animating to its original size (smaller) the view is re-sized instantly and then animated to its designated location.
What I need is that the resizing to the smaller frame is animated as well.
I've tried adjusting the autoresizingMask as well, but that didn't change anything.
Hope someone can help.
thanks.
Michiel, Raj,
I made a youtube tutorial showing how to make views expand and shrink like in the facebook iPhone app.
Hope it can be of some help: How to make expanding/shrinking views on iPhone SDK
Adam
Try this
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationRepeatAutoreverses:YES];
self.theView.frame = frame;
[UIView commitAnimations];
In response to comment;
If you want to control animation reverse, have a flag like BOOL isbig and set it in app launch to NO, then toggle it in every button tap, then check it if it is YES or NO, and according to the result call a method. If NO, call your code, if not call the code again but this time set the frame to the original size.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.theView.frame = originalFrame;
[UIView commitAnimations];
I could solve the problem by suggestion found in other thread, it has its own limitations though. I have posted the code in the same thread where the solution was suggested:
UIView scaling during animation
I worked around this problem when releasing my app by just removing the animation.
It kept bugging me in the back of my mind, so just this week I thought that maybe by using the center property and the size instead of the origin it would make a difference.
So I decided to test this out. Guess what. The problem doesn't occur when using sdk 4.x
Thanks all for trying to help me out.

Flipping UIView inside UIScrollView

In photo app of iphone you can see smooth scrolling to the other picture with finger flicking. Is it possible to allow individual view to be 'flipped' (or rotated) to show its back side ? Found that UIScrollView is used for smooth scrolling and found the code for flipping UI. Outside of UISCrollView I got the flip to work using the code below. But once inside UIScrollView this doesn't work - it executes but UI never switches.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:myView cache:YES];
[UIView setAnimationDuration:1.0];
[myView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];
Perhaps this is not suppose to work this way. What is the recommended way or technique to have flippable individual views inside the UIScrollView ?
Never mind. It does work. Once inside UIScrollView there were several UIViews and I needed to make sure I grab the pointer to current UIView. The above code works in this case as well. I was doing a flip on a view which was hidden :)

Using of a single view to display different content on flip

I am trying to add a flip on row did select of every section.The flip side view should display the content of the selected section. I have tried to understand the apple transition tutorial but its tough to get.
Any simple application related to it .
Have you looked at the Apple Sample code? That is pretty well laid out and has a flip animation. If you create a default utility project, that code has pretty much nothing but flip code in it.
Generating a flip animation is very easy. You simply remove your main view (in your case the UITableView) and add your new view to the superView all inside a UIView animation block:
// start the animation block [UIView beginAnimations:nil context:NULL];
// Specify a flip transition
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[myAppsWindow removeSubView:myUITableView]; // obiously replace these views with the correct views from your app.
[myAppsWindow addSubview:mybackView]; // you should build this view with your details in it.
// commit the animations. The animations will run when this invocation of the runloop returns.
[UIView commitAnimations];