Difference between transitionfromview and addsubview - ios

Is transitionFromView:toView:duration:options:completion: similar to [self.view addSubview:secondView];
RootViewController.m
self.svc = [[secondVC alloc]init];
[UIView transitionFromView:self.view toView:self.svc.view duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {}];
is similar to
RootViewController.m
self.svc = [[secondVC alloc]init];
[self.view addSubView:self.svc];
[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationOptionCurveEaseOut.............
does first approach can lead to memory leaks? is this approach correct?

You should use transitionFromView:toView:duration:options:completion: if you switch from one view to another and want to discard the first one. If you add additional views to your view you should use addSubview:

Related

UIView animateWithDuration ignores duration

My goal is simple and I have achieved this previously which is why I'm resorting to asking my question here.
I am trying to animate a subview. Simple enough. The view ends up exactly where I want it to be after the animation is over. The only problem is:
whatever I do the animation duration is completely ignored. It happens instantly.
- (void)callToDismissView:(UIView *)view {
[self.view addSubview:view];
[self dismissViewControllerAnimated:NO completion:nil];
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
CGRect frame = self.view.frame;
frame.origin.x = frame.size.width;
view.frame = frame;
} completion:^(BOOL finished){
[view removeFromSuperview];
}];
}
Now I have tried different frames and durations to see if the view was animated at all and it always ends up where I need it to be. Only instantly...
EDIT:
The above code is being called by a delegate in the dismissed view controller like so:
- (void)dismissSelf {
if (self.delegate && [self.delegate respondsToSelector:#selector(callToDismissScan)]) {
[self.delegate callToDismissScan];
}
}
And that method itself is triggered by a notification.
I must admit that some things are happening during the transition between the two controllers that I don't fully understand. (And I would very much like to understand them...)
You can try
[self.view layoutIfNeeded];
like this
- (void)callToDismissView:(UIView *)view {
[self.view addSubview:view];
[self dismissViewControllerAnimated:NO completion:nil];
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
CGRect frame = self.view.frame;
frame.origin.x = frame.size.width;
view.frame = frame;
[self.view layoutIfNeeded];
} completion:^(BOOL finished){
[view removeFromSuperview];
}];
}
Maybe you need try like this.
- (void)callToDismissView:(UIView *)view {
[self.view addSubview:view];
CGRect frame = self.view.frame;
frame.origin.x = frame.size.width;
[UIView animateWithDuration:0.3 delay:0.3
options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
view.frame = frame;
}
completion:^(BOOL finished){
[view removeFromSuperview];
//[self dismissViewControllerAnimated:NO completion:nil];//uncomment if need.
}];
}
It seems there was no way in hell I could animate the transition in the presenting view controller and so the simplest way to resolve the issue was to animate the transition in the presented view controller before it is being dismissed.
The way I did this was by casting the delegate to a view controller. It was then easy to add it's view as a sub-view of my presented view controller and animate it like so:
- (void)dismissSelf {
UIView *dummyView = [ScreenCapper captureScreenIn: self];
UIViewController *presentingViewController = (UIViewController *)self.delegate;
[self.view.window addSubview: presentingViewController.view];
[self.view.window addSubview: dummyView];
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){
dummyView.frame = CGRectMake(dummyView.frame.size.width, dummyView.frame.origin.y, dummyView.frame.size.width, dummyView.frame.size.height);
} completion:^(BOOL finished){
if (self.delegate && [self.delegate respondsToSelector:#selector(callToDismissView)]) {
[self.delegate callToDismissView];
}
}];
}
And in the presenting view controller all that was left to do was to dismiss the presented view controller without animations.
- (void)callToDismissView {
[self dismissViewControllerAnimated:NO completion:nil];
}

curlUp and curlDown Animation from one view controller to another

I am trying to go from one view controller to another with the use of page curl up animation; but when my second controller loads, it hides the collection view from it. This is my code:
OffersVC *Offers = [[OffersVC alloc]init];
Offers = [self.storyboard instantiateViewControllerWithIdentifier:#"Offers"];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.view addSubview:Offers.view];
[UIView commitAnimations];
Try to add animation as below and check
UIView *mainview = self.view.window;
OffersVC *Offers = [[OffersVC alloc]init];
Offers = [self.storyboard instantiateViewControllerWithIdentifier:#"Offers"];
[UIView transitionWithView:mainview
duration:kAnimationViewTransition
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self.navigationController.view removeFromSuperview];
[mainview addSubview: offers.view];
}
completion:^(BOOL finished) {
[self presentModalViewController:offers animated:NO];
}
];

UIView Animation at viewDidLoad/viewDidAppear?

I am attempting a simple UIView animation in the viewDidLoad or viewDidAppear method but it doesn't animate.
UIImage *bluredScreenshot = [self.parentViewControllerScreenshot applyBlur];
[UIView transitionWithView:self.screenshotView duration:3.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.screenshotView.image = bluredScreenshot;
} completion:nil];
Simple cross dissolving two images. When ran from viewDidLoad or viewDidAppear the image changes but isn't animated.
But lets say I run the animation from a method after I press a button, it will animate. Why? Seem strange.
Is it possible to make it from the viewDidLoad method? How would you solve this?
Thanks.
You need to fade in the screenshot. If you're using two images, you'll need to place one image view atop the other.
pop this inside viewDidAppear: after calling [super viewDidAppear:animated];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.screenshotView.frame];
[self.view addSubview:imageView];
[imageView setAlpha:0.0f];
imageView = blurredScreenshot;
[UIView animateWithDuration:0.3f animations:^{
[imageView setAlpha:1.0f];
} completion:^(BOOL finished){
self.imageView.image = blurredScreenshot;
[imageView removeFromSuperview];
}];
when the view is loading you can't animate because there isn't anything to animate. Try in viewdidapear: and Don't use transition
[UIView animateWithDuration:3.
delay:0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.screenshotView.image = bluredScreenshot;
}
completion:nil];
Just a reminder, sometimes your problem can be caused by not calling:
self.layoutIfNeeded()

UIViewAnimation Flip not working

In a UIView subclass I have this:
self.frontImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"front"]];
self.backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"back"]];
[self addSubview:self.backImageView];
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[self.backImageView removeFromSuperview];
[self addSubview:self.frontImageView];}
completion:^(BOOL finished){
//nothing
}];
But I don't get the flip animation. The front image view just appears immediately. What am I doing wrong?
You are making a silly mistake using
animateWithDuration:delay:options:animations:completion:
instead of
transitionFromView:toView:duration:options:completion:
Your code should be:
[UIView transitionFromView:backImageView
toView:frontImageView
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished) {
// animation completed
}];

XCode IOS Beginner UIViewController Class change with button

I am new to programming, I have been searching all night long and tyring to figure this out for a long time. Cant.... I dont want to use a navigation Controller but what I am trying to do is this.
So I have two classes in my project. Class1 and Class2. Now there is a button in Class1. If this button is pressed, I want the screen to go to Class2. The closest I got was creating a parent class but when I do that, the button remains on both classes.
- (IBAction)addScheduleB:(id)sender {
[UIView beginAnimations:#"View Curl" context:nil];
[UIView setAnimationDuration:1.00];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if (self.firstScreen.view.superview ==nil){
if(self.firstScreen==nil) {
self.firstScreen = [[FirstScreen alloc] initWithNibName:#"FirstScreen" bundle:nil];
}
[self.config1.view removeFromSuperview];
[self.view insertSubview:self.firstScreen.view atIndex:0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[self.config1.view removeFromSuperview];
[self.view insertSubview:self.firstScreen.view atIndex:0];
[sender setTitle:#"Switch To DVR"];
}
else{
if(self.config1 == nil){
self.config1 = [[Config1 alloc] initWithNibName:#"Config1" bundle:nil];
}
[self.firstScreen.view removeFromSuperview];
[self.view insertSubview:self.config1.view atIndex:0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.firstScreen.view removeFromSuperview];
[self.view insertSubview:self.config1.view atIndex:0];
[sender setTitle:#"Switch To TV"];
}
[UIView commitAnimations];
}
I want to remove the parent class and have just the two classes.
Is it possible to have a button inside class1 when pushed will take me to class2???
There is a 7 minute tutorial on YouTube Here. That shows how to move from one view to another using a button without storybord so you will be able to lern the code to move from one class to another.
I hope this helps. As for being a beginner if you look around youtube you will find lots of tutorials to help you learn. :)
EDIT: this does not use a navigation controller
i hope This solution helps You
just make object from Class2 and when Button1 Pressed add Class2's object.View as subview to Class1.view
Good Luck
Try something like :
- (IBAction)flipViews {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.6f];
if ([saisieCompleteView superview])
{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[saisieCompleteView removeFromSuperview];
[self.view addSubview:saisieRapideView];
[self.view sendSubviewToBack:saisieCompleteView];
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[saisieRapideView removeFromSuperview];
[self.view addSubview:saisieCompleteView];
[self.view sendSubviewToBack:saisieRapideView];
}
[UIView commitAnimations];
}
The both saisieCompleteView and saisieRapideView must be subview of your principal view.
you may pop the another view with modal flip transition:
[self presentViewController:Class2 animated:YES completion:^{
// Some code on completeion
}];
to dismiss the Class2 view:
[self dismissViewControllerAnimated:YES completion:^{
//code
}];
if you don't want to go out of current view controller, you may flip it this way:
BOOL toShowSecond = YES;
...
[UIView transitionWithView:self.view
duration:1.0
options:(toShowSecond?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight)
animations:^{
if(toShowSecond) {
firstView.hidden = YES;
secondView.hidden = NO;
} else {
firstView.hidden = NO;
secondView.hidden = YES;
}
}
completion:^(BOOL finished){
toShowSecond = !toShowSecond;
}];

Resources