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

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

Related

iOS7 Fullscreen uiview.. why disappear status bar?

(i can't speak english well T.T)
I want fullscreen uiview in uiviewcontroller.
I use this code.
- (IBAction)addBtn:(id)sender {
addView = [[AddView alloc] initWithFrame:CGRectZero];
[[[UIApplication sharedApplication]delegate].window addSubview:addView];
addView.window.windowLevel = UIWindowLevelStatusBar;
[addView setAlpha:0.0f];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[addView setAlpha:1.0f];
[UIView commitAnimations];
}
when click button (+)
disappear status bar!!!!!!!
when i use same code in ios6, it's worked.
but in ios7 not worked.
why disappear status bar?
check my sorce code here.
write below code in viewdidLoad function...
your code......///
[addView setAlpha:0.5];// make addsubview alpha 0.5
Hope it will work...my friend!!!!
Happy Coding!!!!!!
Hi here is the code for your addBtn Method.
-(IBAction)addBtn:(id)sender {
addView = [[AddView alloc] initWithFrame:CGRectZero];
[[[UIApplication sharedApplication]delegate].window addSubview:addView];
addView.window.windowLevel = UIWindowLevelNormal;
[addView setAlpha:0.0f];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[addView setAlpha:1.0f];
[UIView commitAnimations];
}
It is working fine now.
You need to remove this line, addView.window.windowLevel = UIWindowLevelStatusBar;
- (IBAction)addBtn:(id)sender {
addView = [[AddView alloc] initWithFrame:CGRectZero];
[[[UIApplication sharedApplication]delegate].window addSubview:addView];
// addView.window.windowLevel = UIWindowLevelStatusBar;
[addView setAlpha:0.0f];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.35f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[addView setAlpha:1.0f];
[UIView commitAnimations];
}

how to dismiss a subview after checking whether it is already added, with the same button action?

I use the following code to add a date picker view to my app main view, on clicking a button.
- (IBAction)CalenderButton_Click:(id)sender
{
// code to add date picker -mydatepicker
//animating
[self.view addSubview:myPicker];
CGRect onScreenFrame=myPicker.frame;
CGRect offScreenFrame=onScreenFrame;
offScreenFrame.origin.y=self.view.bounds.size.height;
myPicker.frame=offScreenFrame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
myPicker.frame=onScreenFrame;
[UIView commitAnimations];
}
two things i need to implement is,
1.this code animates the view from bottom, how can i animate it from top??
2.if i click the button to add date picker again, i should check whether the view is already added, if yes, remove the sub view.
Thanks in advance
For question number 2: Use a boolean value to check whether the date picker is already in the view.
isSubView is a BOOL to check ,your myPicker is in the subview.
UPDATED:
- (IBAction)CalenderButton_Click:(id)sender
{
//animating
if(!isSubview){
isSubview = YES;
[self.view addSubview:myPicker];
/*
CGRect onScreenFrame=myPicker.frame;
CGRect offScreenFrame=onScreenFrame;
offScreenFrame.origin.y=self.view.bounds.size.height;
myPicker.frame=offScreenFrame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
myPicker.frame=onScreenFrame;
[UIView commitAnimations];
*/
// to slide your view from the top
[myPicker setFrame:CGRectMake(0, -200, 320, 200)];
[myPicker setBounds:CGRectMake(0, 0, 320, 200)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
[myPicker setFrame:CGRectMake(0, 0, 320, 200)];
[UIView commitAnimations];
}else{
isSubview = NO;
[myPicker removeFromSuperView];
}
}

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

Why do I see jerky animation when using this UIView animation block?

I have a a tab bar that is made in the application delegate. By calling an action form a button click from one of the views loaded from the tab bar, I open the help screen but there is a jerking motion after loading.
forgive me for speaking informally..I have been picking my brain for the past few hours trying to figure this out..
-(void)flipToHelp {
HelpViewController *helpVariable = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[self setHelpViewController:helpVariable];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:_window cache:YES];
[_window removeFromSuperview];
[helpVariable release];
self.window.rootViewController = self.HelpViewController;
[UIView commitAnimations];
}
Just to reiterate from the comment thread, you shouldn't be removing the window from its superview (it doesn't technically have a superview, so it's probably causing problems). And setting the window's rootViewController property should swap out the view hierarchies, Apparently the jerkiness comes from changing the window's rootViewController property, so maybe the solution is to avoid using that property. Here's what I think should be enough to accomplish this:
-(void)flipToHelp {
HelpViewController *helpVariable = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_window cache:YES];
[self.tabBarController removeFromSuperview];
[_window addSubview:helpVariable];
[UIView commitAnimations];
}
-(void)flipToHelp {
HelpViewController *helpVariable = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[self setHelpViewController:helpVariable];
[helpVariable release];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.window
cache:YES];
self.window.rootViewController = self.HelpViewController;
[UIView commitAnimations];
}
How about this code? Does it still have a jerky animation?

Scaling a UIView during transitionFromView?

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