iPhone animated view overlay - iphone

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

Related

uiview CGAffineTransform animates backwards

I have a UIView subclass which I am able to successfully use UIView animations on.
This UIView also has subviews, which I also want to be able to UIView animate.
However, it seems that whenever I create an animation and apply it to the parent view it works fine, but if I apply the animation to the child view it animates backwards.
For example, if I scale my UIView parent(self) by a factor of 2 (to double the width and height):
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration:2];
[UIView setAnimationDelay:2];
CGAffineTransform scaleTransform = CGAffineTransformScale( self.transform, 2, 2 );
self.transform = scaleTransform;
[UIView commitAnimations];
then this works fine.
But if I do the same on the child:
UIView *myObj = [[self subviews] objectAtIndex:0];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration:2];
[UIView setAnimationDelay:2];
CGAffineTransform scaleTransform = CGAffineTransformScale( myObj.transform, 2, 2 );
myObj.transform = scaleTransform;
[UIView commitAnimations];
It doesn't work. What happens visually is that the child view immediately shrinks down and then after the delay scales back up to it's original size.
If I log the transform parameters it looks the same for both parent and child and I have tried setting CGAffineTransformIdentity.
Any ideas what might be the problem?
I was overriding layoutSubviews and in there I was assigning sizes/positions of the views based upon device orientation which was conflicting.doh!

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 UIView Question. How to get notified when a UIView becomes completely hidden?

I have a have a strip of UIViews that slides horizontally behind a UIView "window". Only the UIViews within the bounds of the "window" are seen. As a view becomes hidden I would like to be notified so that I can perform some task with the just hidden view. What is the best way to do this?
In your UIScrollViewDelegate:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// left and right bounds of the subview in relation to the scrollview
CGFloat left = mySubview.frame.origin.x - myScrollView.contentOffset.x;
CGFloat right = left+mySubview.frame.size.width - myScrollView.contentOffset.x;
if(right<=0 || left>=myScrollView.frame.size.width){
// The view is not visible
}
}
this checks if either the left or right side of the subview is visible.
Add a callback selector to your animation:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:theView cache:NO];
[UIView setAnimationDidStopSelector:#selector(animationDone)];
theView.frame = newFrame;
[UIView commitAnimations];

how to implement an iPhone view transition animation with both flipping and scaling?

how can I implement the animation we see in the iPhone Music app's coverflow screen? when you click on a small view, it flips and scales up to another view? how can I do this? I can use core animation to flip and scale a view, but how can I do the transition to another view? thanks
You need an UIView as Container for the two UIViews (frontside/backside) and then remove/add these from/to the container as subviews while doing the animations in between:
UIView *flipContainer;
UIView *frontSide;
UIView *backSide;
//...
-(void)turnUp
{
[backSide removeFromSuperview];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:flipContainer cache:YES];
[UIView setAnimationDuration:1.0];
CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
flipContainer.transform = transform;
[UIView commitAnimations];
[flipContainer addSubview:frontSide];
}
-(void)turnDown
{
[frontSide removeFromSuperview];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:flipContainer cache:YES];
[UIView setAnimationDuration:1.0];
CGAffineTransform transform = CGAffineTransformMakeScale(1, 1);
flipContainer.transform = transform;
[UIView commitAnimations];
[flipContainer addSubview:backSide];
}
I'm trying the exact code you are doing - I get a zoom effect but no turn over. The only difference is that right before the turnUp code I add the flipContainer (with back showing) so then it can be flipped over.
// construct animation container
self.flipContainer = [[FlipContainer alloc] init];
[self.flipContainer.view setFrame:CGRectMake(clickedSquareX, clickedSquareY, 200, 200)];
[self.flipContainer.view addSubview:self.backside.view];
// add animation container
[self.myParentView.view addSubview:self.flipContainer.view];
// PROCEED to your turnUp code
The reason I'm doing this is I have a bunch of images in a horizontal UIScrollView and so to 'simulate' a 200x200 image flipping over and zooming to show detail I add my flipContainer with the backside showing the exact image over the exact spot of the pressed image. It should work shouldn't it? A bit confusing to me is the first line of your turnUp code you do:
[backSide removeFromSuperview];
..which would remove the view I just added.
I'm not sure if this is the right spot to put this question in - sorry if it isn't!

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