Animate resize of view during rotation without autoresize - iphone

If I set the AutoResize mask of my view, then they will resize together with the main window during device rotation.
Is there a way to get the same smooth performance by using my own animation and no autoresize?

Yes, you can certainly use UIView animations to move elements around.
Also, a common technique is to hide the interface as part of
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
redraw it and show it as part of
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
Here is an example:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
self.myTable.hidden = YES;
...
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self redrawInterface];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
self.myTable.hidden = NO;
...
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
}

Related

UIView shows the subview view sometimes?

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;

animate textlabel in uitableviewcell using willTransitionToState

I am trying to animate the textlabel in a UItableviewcell when I press the edit button.
I am trying to make it fade out and fade in.
fading in works but when I press 'edit' the textlabel disappears and when I press on 'done' I fades in just perfectly.
Can anyone tell me why it isn't working?
thanks in advance
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if ((state & UITableViewCellStateEditingMask) || (state & UITableViewCellStateShowingDeleteConfirmationMask)) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
label.alpha = 0.0;
[UIView commitAnimations];
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
[super didTransitionToState:state];
if (!(state & UITableViewCellStateEditingMask) && !(state & UITableViewCellStateShowingDeleteConfirmationMask)) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
label.alpha = 1.0;
[UIView commitAnimations];
}
}
I noticed that when entering willTransitionToState that animations were disabled. The following fixed it.
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
//Should be enabled by default...but apparently not
[UIView setAnimationsEnabled:YES];
...
}
From everything I had read I thought for sure the willTransitionToState was the way to go. It even works perfectly if you use didTransitionToState though the transition starts after the normal editing transition finishes.
As it turns out I think you want to use setEditing
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if(editing) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
label.alpha = 0.0;
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
label.alpha = 1.0;
[UIView commitAnimations];
}
}

Fade-in / fade-out during an interface rotation

When my iPhone interface rotates, I would like to do a fade-in/fade-out for a specific UIView of a UIViewController... Like...
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
theView.alpha = 0;
[UIView commitAnimations];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
theView.alpha = 1;
[UIView commitAnimations];
}
But the animation doesn't finish before the rotation start (we can see the view starting to self-resize)...
Is there a way to delay rotation start ?
"duration" is the duration of the rotating animation, right ?
I found that running the current run loop for the same amount of time as the preceding animation, did in fact delay the rotation.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[UIView animateWithDuration:0.25 animations:^{
theview.alpha = 0.0;
}];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}
Your problem stems from the fact that by the time willRotateToInterfaceOrientation: is called, the view being rotated already has its orientation property set and the animation block handling the rotation is also ready to run on a separate thread. From the documentation:
This method is called from within the animation block used to rotate the view. You can override this method and use it to configure additional animations that should occur during the view rotation.
I would suggest overriding the shouldAutorotateToInterfaceOrientation: method to fire your animation before returning YES for supported orientations:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (interfaceOrientation == (UIDeviceOrientationPortrait || UIDeviceOrientationPortraitUpsideDown) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
theView.alpha = 0;
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
theView.alpha = 1;
[UIView commitAnimations];
}
return YES;
}
This should ensure the animation runs before you ever set the orientation of UIViewController and fire the rotation animation. You may have to add a slight delay to get the effect your are looking for depending on the device hardware speed.

orientation change

I need to show fade-in kind of transition effects while the view changes its orientation from one mode to other. Can anyone suggest me how to get that?
I would use an animation block inside of this method like so:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView beginAnimations:#"InterfaceFade" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:duration];
// Change properties to animate here (Ex: view.alpha = 0.0)
[UIView commitAnimations];
}

iPhone: transitioning views with Core Animation

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];