orientation change - iphone

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

Related

Animate resize of view during rotation without autoresize

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

UIView cancel animation

I currently have a view animating from one part of the screen to another. I want the animation to pause whenever the "pause" button is pressed. This means the animation needs to remain right where it is (frame location) when I "pause" the animation. I've tried a few things but nothing has given me the result I want. Below is a rough version of my current code.
-(IBAction)startScroll
{
NSLog(#"Start scroll");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:30];
//set new frame for view
[UIView commitAnimations];
}
and one method to stop the animation
-(IBAction)pauseScroll
{
NSLog(#"Pause Scroll");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.1];
//Pause animation in it's current location
[UIView commitAnimations];
}
Ended up going with a timer and updating the contentOffset every few seconds. Wish there was a better way to get a view current coords even during an animation. The presentationLayer stuff did not yield successful results.
UIView animations don't quite work like that. Once you set the frame, the view instantly moves there from your app's perspective; it's just the display to the user that's animated.
You can use the "presentation layer" to figure out roughly what's being displayed on screen (it's only approximate since the compositing happens in another thread):
#import <QuartzCore/CALayer.h>
...
[fooView setFrame:fooView.layer.presentationLayer.frame]

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.

iPhone animated view overlay

I have a few XIBs that are my pages, and i'm using presentmodalviewcontroller to switch between them with some buttons. Now I want to use other buttons to pull up overlays on those views. How I have it now is I have a button which simply toggles the "Hidden" property on the UIImageview.
What are options for animating the show/hide functionality of this? I'd like some sort of zoom-in or zoom-out effect when the overlay is called up rather than just suddenly showing/hiding.
-(IBAction)basketballbutton{
if (basketball.hidden == YES)
basketball.hidden = NO;
else if (basketball.hidden == NO)
basketball.hidden = YES;
Thanks!
You can animate the view opacity using the alpha property.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
[basketball setAlpha:([basketball alpha] > 0.0) ? 0.0f : 1.0f];
[UIView commitAnimations];
This will animate the showing and hiding of the view.
Here is how you set the transform for scaling:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
// Scale up 2x
[basketball setTransform:CGAffineTransformMakeScale(2.0f, 2.0f)];
[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];