Scaling a UIView during transitionFromView? - iphone

I'm using a container UIView to replicate the kind of behavior the iTunes store does when you tap on album artwork and it flips and scales.
Current code looks like:
//mainView is 300x300x, smallView is 30x30
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView transitionFromView:mainView toView:smallView duration:3.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
containerView.frame = CGRectMake(275, 415, 30, 30);
[UIView commitAnimations];
I can't seem to get the content of the containerView to scale during the animation, the frame just closes in on the content. I tried applying some transforms to both the view and the layers and a bunch of other things but I can't seem to get it to behave properly.

Rather than setting the frame, try using a transform instead:
- (void)setStartTransform:(CGAffineTransform)transform;
- (void)setEndTransform:(CGAffineTransform)transform;
Something like
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView transitionFromView:mainView toView:smallView duration:3.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
//containerView.frame = CGRectMake(275, 415, 30, 30);
[containerView setStartTransform: CGAffineTransformIdentity];
[containerView setEndTransform: CGAffineTransformMakeScale(0.1, 0.1)];
[UIView commitAnimations];
(You might need to apply a translate to the transform too.)

Try it this way:
[UIView transitionWithView:mainView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
containerView.frame = CGRectMake(275, 415, 30, 30)
}
completion:NULL];

Related

UIView Animation not working properly in IOS 7

I have a method to animate and reset the view which is given below.
-(void)animateToFocus:(BOOL)animate index:(NSInteger)index {
if (animate) {
[UIView beginAnimations:#"Scroll" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationsEnabled:YES];
self.view.frame = CGRectMake(0, -50*index, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView commitAnimations];
} else {
[UIView beginAnimations:#"Scroll" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationsEnabled:YES];
self.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView commitAnimations];
}
}
When I pass YES and a tag in this method, it will animate to top and when I pass NO it will reset the view.
It works perfectly in IOS6.When i update to IOS 7, resetting done as:
It will not animate completeley and a black space appear in the bottom. Can u please help me to solve this?
I'm guessing that's a UITableView inside?
If so, you shouldn't be animating it's frame you should use:
– scrollToRowAtIndexPath:atScrollPosition:animated:
or:
– scrollToNearestSelectedRowAtScrollPosition:animated:

How to make an UIView come top to bottom and vice versa on drag

I am trying to do an animation where a UIView should come from top to bottom(or till mid of screen) when we toch on top of the screen and drag down.
Set your view frame, and then add these code in your button action or else.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:1.0];
[yourView setFrame:CGRectMake(0, 250, 320, 100)];
[UIView commitAnimations];
set positions according to ur need
[UIView beginAnimations:#"DragView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.5f];
self.dragView.frame = CGRectMake(newXPose, newYPose,width, height);
[UIView commitAnimations];

iPhone:Animation move view controller upward

I am developing an iPhone application, where i need to move a first view controller slowly upward in animation and move to second view controller. I am thinking to use CoreAnimation for moving the first view controller slowly upward and push to next view controller. Could someone help on giving what are the classes/apis available to achieve this?
Thank you!
Try this,
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:yourfirstview cache:YES];
[UIView setAnimationDidStopSelector:#selector(remove_view)];
//change frame here
yourfirstview.frame =CGRectMake(0,-480,320,480);
[UIView commitAnimations];
-(void)remove_view
{
[yourfirstview removeFromSuperview];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:yoursecondview cache:YES];
//change frame here
yoursecondview.frame =CGRectMake(0,0,320,480);
[UIView commitAnimations];
}
You can easily achieve this on a single view controller using multiple UIViews
As per Apple's documentation:
In iOS 4 and later, use the block-based animation methods.
So, in order to produce the intended results, using the following code.
UIView *mySecondView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 480)];
[mySecondView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:mySecondView];
[UIView animateWithDuration:2.0 delay:3.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[mySecondView setTransform:CGAffineTransformMakeTranslation(0, -480)];
}completion:^(BOOL done){
}];

Looking for a within app transition (revolving screen effect)

I want a transition within my app that is like the mystery revolving wall from old scooby doo cartoons. I want the screen to revolve when switching views. Anyone point me in the right direction for the possibility of accomplishing this?
Or this, which uses far less ink:
UIView *bookCaseView; // this is the container... the haunted wall
UIView *booksView; // just an ordinary set of books, right?
UIView *spookyBackside; // ruh-roh, raggy!
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[booksView removeFromSuperview];
[bookCaseView addSubview:spookyBackside]; }
completion:NULL];
I think this is what you are looking for:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
//optional if you want to do something after the animation
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(myAnimationDidFinish:finished:context:)];
//
[view2 setFrame:CGRectMake(0, 0, view2.frame.size.width, view2.frame.size.height)];
[view1 addSubview:view2];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view1 cache:YES];
[UIView commitAnimations];
And to flip back:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
//optional if you want to do something after the animation
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(myOtherAnimationDidFinish:finished:context:)];
//
[view2 removeFromSuperview];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view1 cache:YES];
[UIView commitAnimations];

iPhone Animation : How to animate only subview ? Not the whole view?

I am new with animations and I am using following code for Flip animation.
//MyMapView is UIView
MyMapView *aMapView = [[MyMapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300) AndResArray:self.nearMeArray];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[[self view] addSubview:nearMeMap];
[UIView commitAnimations];
Its working fine. But whole view (with super view) Flips.
I only want to Flip subview that is MyMapView. The super view should not flip.
How to do this ???
EDITED
screen :
Below is main screen. When click on Map button I want to Flip only Brown part of the screen. Whole screen should not Flip.
Thanks...
I have figure out what's the problem. Please see the below code.
MapView.m
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
[self performSelector:#selector(addMapView) withObject:nil afterDelay:2.0];
}
return self;
}
- (void) addMapView {
//MyMapView is UIView
MKMapView *aMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 321, 300)];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
[self addSubview:aMapView];
[UIView commitAnimations];
}
And after that simply add object of MapView class where you want. like below.
//MyMapView is UIView
mapView *aMapView = [[mapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300)];
[[self view] addSubview:aMapView];
vote up if this comment help you.
Thanks,
MinuMaster.
Matteo's answer is partially correct, but you need to set the animation on the view before you add it, or it won't work correctly. Like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:aMapView
cache:YES];
[self.view addSubview:aMapView];
[UIView commitAnimations];
Alternatively, you can use the block-based API introduced in iOS 4. Here's an example.
[UIView transitionWithView:aMapView
duration:1.0
options:UIViewAnimationTransitionFlipFromRight
animations:^{ [self.view addSubview:aMapView]; }
completion:^(BOOL f){ }
];
You need to add your view to self.view and then flip only aMapView:
MyMapView *aMapView = [[MyMapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300) AndResArray:self.nearMeArray];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[self.view addSubView:aMapView];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:aMapView cache:YES];
[UIView commitAnimations];
Animated transitions like this animate the contents of a view. The system takes a snapshot of the contents at the beginning of the animation, another snapshot when you commit, and then animates between them. As you've seen with your original code, the entire view gets animated.
The simplest solution is probably just to introduce an intermediate view that's the size and position of where the map will go. Then animate that view as you add the map to it.
More info in the View Programming Guide's section on Creating Animated Transitions Between Views. Though the methods you're using have, in iOS 4, been superceded by new block-based versions.
-(void)viewDidLoad
{
[super viewDidLoad];
OptionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
OptionView.backgroundColor=[UIColor blueColor];
OptionIsShow=false;
MoveView=[[UIView alloc] initWithFrame:CGRectMake(40, 40, 70, 70)];
[MoveView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:MoveView];
}
-(void)ViewOrHideOptionView
{
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.MoveView cache:NO];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0];
if(!OptionIsShow)
{
[self.MoveView addSubview:OptionView];
OptionIsShow=true;
}
else
{
[self.OptionView removeFromSuperview];
OptionIsShow=false;
}
[UIView commitAnimations];
}