UIView animation doesn't work for UIViewController subclass - ios

I try to add a subview(which is a subclassed UIViewController's view, consists of several UIButtons and UILabels) to current view, but the animation doesn't work, the subview just appeared without animation.
the code is :
[UIView beginAnimations:nil context:nil];
[UIView setAnimaitonCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationTransition:transition forView:thisPersonViewController.view cache:YES];
[self.view addSubView:thisPersonViewController.view];
[UIView commitAnimations];
it just doesn't work, but if I change forView parameter from the subview (thisPersonViewController.view) to self.view, the self.view did animate, that's curios.
My xcode version is 4.2 and SDK version is 5.0, thx for anyone who offer an solution!
===========THE FOLLOWING CODE WORKS FINE===============
CATransition *trans = [CATransition animation];
[trans setDuration:0.4f];
[trans setType:kCATransitionReveal];
[trans setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[thisPersonViewController.view layer]addAnimation:trans forKey:kCATransitionReveal];
[self.view addSubview:thisPersonViewController.view];
Another annoying question is if I change the CATransition type to kCATransitionFromBottom or something else the animation doesn't work again !

Add thisPersonViewController.view as a subview with a position somewhere off the screen. (i.e. frame x/y coordinate right at the bottom of the screen.)
Do this:
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationCurveLinear animations:^(void)
{
// Move it to wherever you want to have it.
thisPersonViewController.view.frame = CGRectMake(0.0f, 0.0f, thisPersonViewController.view.frame.size.width, thisPersonViewController.view.frame.size.height);
}
completion:^(BOOL finished)
{
// Do something when it completes the animation if you so desire.
}];

Related

Make popup of dataDetectorTypes (Textview) in front

i have a line separator of master and detail view(ipad) and stay always appeared when touching a textview , is that a way to make always the popup in front ?thanks..
for that you have to Create one UIView & Use like ,
//For Set Alert View
[[self.view superView] bringSubviewToFront: YOurCreatedView];
& for Dismiss Alert View
[[self.view superView] sendSubviewToBack: YOurCreatedView];
And For Animation Use below Method..
[UIView animateWithDuration:0.25 animations:^{
//
} completion:^(BOOL finished) {
//
}];
or Below Method..
CATransform3D transform3d = CATransform3DMakeScale(1.15, 1.15, 1.15);
cell.layer.transform = transform3d;
//4. Define the final state (After the animation) and commit the animation
[UIView beginAnimations:#"translation" context:NULL];
[UIView setAnimationDuration:0.8];
[[self.view superView] bringSubviewToFront: YOurCreatedView];
YOurCreatedView.alpha = 1;
YOurCreatedView.layer.transform = CATransform3DIdentity;
YOurCreatedView.layer.shadowOffset = CGSizeMake(0, 0);
[UIView commitAnimations];

How do I animate my subviews on adding them?

I have developed a font selection that utilizes UIPickerView and UIToolbar, which are both added on touching a UIButton.
But they just kind of pop in which looks sloppy.
is there a way to animate them?
here is the method that adds them (it's called on clicking the button) :
-(void)showPicker
{
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
}
You can animate them in a couple of ways, but probably the easiest is to just fade them in.
Fade In
[someView setAlpha:0]
[self.view addSubview:someView];
[UIView beginAnimations:#"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:1];
[UIView commitAnimations];
Fade Out
float fadeDuration = 0.5;
[UIView beginAnimations:#"FadeOut" context:nil];
[UIView setAnimationDuration:fadeDuration ];
[UIView setAnimationBeginsFromCurrentState:YES];
[someView setAlpha:0];
[UIView setAnimationDidStopSelector:#selector(removeView)];
[UIView commitAnimations];
-(void) removeView {
[someView removeFromSuperview];
}
Something as simple as that.
Depends on what type of animation you want to use. For example, this would look like dissolve.
Anyways look into UIView animateWithDuration...
pickerTB.alpha = _fontPicker.alpha = 0;
[self.view addSubview:_fontPicker];
[self.view addSubview:pickerTB];
[UIView animateWithDuration:1 animations:^{_fontPicker.alpha = 1; pickerTB.alpha = 1}];
You could use transforms or change frame origins to make the views fly in from positions outside the screen too. In that case, inside the animations block, specify the new on screen position. You can do this in two ways, change the frame with an updated origin or apply a CGAffineTransformMakeTranslation(dx,dy)
You need to add the subview with a frame value that is where you want the animation to begin from (off-screen?) then change the frame value inside your animation block. The frame will change over the duration, causing the appearance of movement. The view must already be a subview - I don't believe adding a subview is something you can animate, it makes no sense.
-(void) showPicker{
[self.view addSubview: _fontPicker];
_fontPicker.frame = CGRectMake(0, 0, 120, 40);// somewhere offscreen, in the direction you want it to appear from
[UIView animateWithDuration:10.0
animations:^{
geopointView.frame = // its final location
}];
}
Initially set your picker view frame and tool bar frame like this one..
-(void)viewDidLoad
{
[super viewDidLoad];
pickerTB.frame = CGRectMake(0,568,320,44);
fontPicker.frame = CGRectMake(0, 568, 320, 216);
}
-(void)showPicker
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.30];
if(isiPhone5)
{
pickerTB.frame = CGRectMake(0,288,320,44);
fontPicker.frame = CGRectMake(0, 332, 320, 216);
}else{
pickerTB.frame = CGRectMake(0,200,320,44);
fontPicker.frame = CGRectMake(0,244, 320, 216);
}
[UIView commitAnimations];
}

IOS Animations without navigation controller

In my app im showing different screen this way:
info = [[Info alloc]initWithNibName:#"Info" bundle:nil];
[self.view addSubview:info.view];
Info is a subclass of UIViewController, so i just create it and add it as a subview of the current view.
When i want to dismiss it i just do:
[self.view removeFromSuperview];
Im not sure if this is the right way of doing it, but yesterday apple has published on app store another app that do the same to display views.
My question is, how can i animate the transition between views, so when i call addsubview it do the animation?
I tried this:
info = [[Info alloc]initWithNibName:#"Info" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:info.view
cache:YES];
[self.view addSubview:info.view];
[UIView commitAnimations];
But its not working, this dont do anything.
It's not the right way to manage view hierarchy. You should use view controller methods for this (addChildViewController: & removeFromParentViewController). Then you can use method transitionFromViewController:toViewController:options:animations:completion: (or something like that) to do various animations.
to add a view controller's view use this
ChildViewController * child = [[ChildViewController alloc] initWithNibName:#"ChildViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:info.view
cache:YES];
[self.view addSubview:child.view];
[UIView commitAnimations];
[self addChildViewController:child];
[child release];//for NON ARC
to remove view use this...
for (UIViewController *childVC in [self childViewControllers])
{
[childVC removeFromParentViewController];
}
[self.view removeFromSuperview];// YOU CAN ADD ANIMATIONS HERE
[self removeFromParentViewController];
HOPE this will help you, It is working fine for me....
You can use CATransition for that kind of animation without using navigationcontroller below is the code
Info *infoViewController = [[Info alloc]init];
CATransition *transition = [CATransition animation];
transition.duration = 0.50;
transition.timingFunction =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromRight;
UIView *containerView = self.view.window;
[containerView.layer addAnimation:transition forKey:nil];
[self presentModalViewController:infoViewController animated:NO];
Like this you can animate the view from left to right without using navigationcontroller.
You can give UIView Block Animation a shot. Something like this:
[UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
requiredView.frame = CGRectMake(0, 300.0, 400.0, 180.0);
} completion:^(BOOL finished)
{
}];
You can set the view's frame and position it outside the viewing co-ordinates and then change the frame accordingly to animate the UIView.

getting access EXC_BAD_ACCESS when doing animation

I am doing an animation, so when you click a button MyAccount from the first screen it will navigate you to Account View
- (void)pushToAccount
{
AccountViewController *controller = [[AccountViewController alloc] initWithNibName:#"AccountViewController" bundle:nil];
//[self.navigationController pushViewController:controller animated:NO];
[UIView beginAnimations: #"Showinfo"context: nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.view addSubview:controller.view];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
[UIView commitAnimations];
}
However, whenever i click on the Account View Controller ( contains some buttons and images view in it ), my program is crashing at it show EXC_BAD_ACCESS at main class
Please advice me on this issue...
Your view controller is trying to animate a transition by adding another view controller's view as a subview of your current view. That's problematic on a whole bunch of dimensions. Notably, you're not doing anything with the new view controller itself ... if this was an ARC project, it will get released on you.
If you just want to transition from one view controller to another, you should generally either do a standard pushViewController:animated: or presentModalViewController:animated:. It looks like you used to do the former. Why did you replace it with the current code?
If you can tell us what you were trying to do, why you're not using the standard transitions, perhaps we can help you further.
Update:
If you don't like the default animation, but rather want some custom animation to your transition, you can do something like:
CustomAnimationViewController *yourNewViewController = [[CustomAnimationViewController alloc] initWithNibName:#"CustomAnimationView" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController:yourNewViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[UIView commitAnimations];
// if you're using ARC, you don't need this following release, but if you're not using ARC, remember to release it
// [yourNewViewController release];
Update 2:
And, anticipating the logical follow-up question, how do you animate the dismissal of the new view controller, you might, for example have a "done" button that invokes a method like the following:
- (IBAction)doneButton:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:0.375];
[self.navigationController popViewControllerAnimated:NO];
[UIView commitAnimations];
}
My guess is that you have a UIButton that will call your pushToAccount method when pressed and that when it is pressed it cannot find that method. It coud be because the method signature does not include an object. So if you change your method from
- (void)pushToAccount
to
- (void)pushToAccount:(id)sender
It may fix your problem. It is also important to make sure that the object that contains the pushToAccount method isn't dealloc'd before that method is called. Perhaps put an NSLog message or a breakpoint inside dealloc to make sure it isn't called.
i think use bellow line
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];
insted of your bellow line..
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
hope this help you...
:)
and also for animation use bellow code if required....
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionFromBottom];
[animation setDuration:1.0];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[[self.window layer] addAnimation:animation forKey:kAnimationKey];
here use your view insted of window otherwise windows workfine..
:)

Animating removeFromSuperview

I'd like to animate the transition from a subview back to the super view.
I display the subview using:
[UIView beginAnimations:#"curlup" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.view addSubview:self.mysubview.view];
[UIView commitAnimations];
The above works fine. It's going back to the super view that I don't get any animation:
[UIView beginAnimations:#"curldown" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
Is there something different I should be doing to get the subview to animate when removed?
If you're targeting iOS 4.0 upwards you can use animation blocks instead:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
(above code comes from Apple's UIView documentation)
I think you need to do forView:self.view.superview instead, to be consistent with what you are doing when you are adding, because in this case the self.view is the child, and so you would need to do it on the parent.
JosephH answer in Swift:
UIView.animateWithDuration(0.2, animations: {view.alpha = 0.0},
completion: {(value: Bool) in
view.removeFromSuperview()
})
Although approach with sending removeFromSuperview message from animation completion block works fine for most cases, sometimes there is no way to prevent a view from immediate removal from view hierarchy.
For example, MKMapView removes its subviews after it receives message removeAnnotations, and there is no "animated" alternative for this message in the API.
Nonetheless the following code allows you to do whatever you like with a visual clone of a view after it's been removed from superview or even deallocated:
UIView * snapshotView = [view snapshotViewAfterScreenUpdates:NO];
snapshotView.frame = view.frame;
[[view superview] insertSubview:snapshotView aboveSubview:view];
// Calling API function that implicitly triggers removeFromSuperview for view
[mapView removeAnnotation: annotation];
// Safely animate snapshotView and release it when animation is finished
[UIView animateWithDuration:1.0
snapshotView.alpha = 0.0;
}
completion:^(BOOL finished) {
[snapshotView removeFromSuperview];
}];
Example below:
func removeSpinningGear(cell: LocalSongsCollectionViewCell) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveLinear, animations: {
cell.spinningGearBtn.alpha = 0.0
}) { _ in
cell.spinningGearBtn.removeFromSuperview()
}
}

Resources