I have a project in which I'm switching one view with another:
- (IBAction)onClick:(id)sender
{
ViewControllerSecond * sc=[[ViewControllerSecond alloc]initWithNibName:#"ViewControllerSecond" bundle:nil];
[UIView transitionFromView:self.view toView:sc.view duration:3.0
options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
}];
}
I'm using 3 seconds here to make a point. in this second view I have a method to update the GUI that adds another view from a view controller:
-(void)updateGUI
{
sample=[[ViewControllerSample alloc]initWithNibName:#"ViewControllerSample" bundle:nil];
sample.view.frame=CGRectOffset(sample.view.frame, 0, 150);
[self.view addSubview:sample.view];
}
Now, here is the problem: when I'm calling this from the viewDidLoad function - it's working just fine.
However, if called from the viewWillAppear function, the view will appear at the top of the screen and only after the animation has ended will jump to it's position.
How can it be fixed?
Whats wrong putting it in viewDidLoad?
From the looks of it, the order is loading, animating then the delegate methods of appearing. Also put your sample=[[ViewControllerSample alloc]initWithNibName:#"ViewControllerSample" bundle:nil]; in the init part of your Second view controller.
Alternatively, you can call updateGUI before your animation. So before [UIView transition...
[sc updateGUI];
Just call updateGUI in viewDidLoad. Don't do much work in viewWillAppear. viewWillAppear will prevent your view's appear if you make it do too much work.
The answer for that was of two parts:
update when the view is loaded, however this will only happen once, so to re-use the controller I have to call it again.
use the isViewLoaded to see if the view is loaded. if it is, then call the update method.
Related
I have a parent UIView and an UITextView as one of the subviews.
And I created a button to dismiss the parent UIView like this:
-(void)cancelButtonPressed:(UIButton *)sender
{
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.frame = CGRectZero;
} completion:^(BOOL finished) {
if (finished) {
[self removeFromSuperview];
}
}];
}
I can tell that the parent UIView didn't get released because if I typed some text into the UITextView and dismissed it, when I opened the UIView again, instead of a blank UITextView, the SAME text is in it again.
I checked the Leaks tool but I didn't see any leaking. So I'm guessing if I have some kind of retain cycle or what.
UPDATE:I have another object (which is the AppDelegate) who is holding the UIView's instance: _myView as a global variable like this:
_myView = [[MyView alloc] init];
_myView.nameLabel.text = _user.screen_name;
[_window addSubview:_myView];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_myView.frame = CGRectZero;
} completion:nil];
But in order to avoid retain cycle, should I create a weak self like this: __weak MyView *weakSelf and in the animation block do this: [weakSelf removeFromSuperview]?
I've also tried calling removeFromSuperview on the view itself, and it doesn't result in the view being released.
If you want to release the view, then go with an approach that uses a delegate. That way, you will be able to call removeFromSuperview on the view, once the animation is complete, and set it to nil. This has worked for me in the past.
So, you can add a method to the view class that you want to animate closed, where you will do the animation. Set your view controller as a delegate to your view, and call some method on the delegate, from the completion block of that animation.
You can create your own protocol for this. If you keep it general enough, and focus only on animation callbacks, you can reuse the protocol in all your view controllers.
Memory management and logic are independent things. A memory leak will never change the behavior of your program. Behavior like displaying something is controlled by what you tell it to display. If it displays the same thing as before, then you must be giving it the same thing to display somehow. Even if you somehow leaked the original thing, if you pass a new thing for it to display, it will display that thing. So look at your logic. Memory management has nothing to do with what you're seeing.
I have a UIViewController subclass that contains a UICollectionView. Selecting a cell presents a new view controller. When I return to the first view controller, the contentOffset of the collection view is reset to CGPointZero.
From my research this seems to be standard behaviour.
I can reset the contentOffset by adding a private property to my view controller subclass, saving the contentOffset in the viewWillDissapear method and resetting it on the collection view in the viewWillAppear method.
I would like to know though, is there another way to prevent the scroll view content offset from being reset in the first place (removing the need for an extra property)?
I am targetting iOS7.
The 2nd view controller is presented like this:
[self presentViewController:secondVC animated:YES completion:nil];
And dismissed like this (in the 2nd view controller):
-(void) dismiss
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Edit: After further investigation it appears resetting the contentOffset is not the default behaviour. I haven't figured out why it is happening in my application yet. I am presenting just as have showed in the code above.
There are 2 options. If your application works with UINavigationController, it should keep the last accessed cell, not sure about how you have worked with. If not, you can always create a notification and in the moment you are getting back to the previous screen, send the update of the UICollectionView or whatever you have and scroll to the correct position.
Bear in mind, the UIViewController life cycle and how it´s going to appear before to update the user interface.
You can create a CGPoint object in AppDelegate. Then you can set that value to your CollectionView objects contentOffset while present and dismiss like that;
[self presentViewController:secondVC animated:YES completion:{
appDelegate.tempContentValue = _collectionView.contentOffset;
}];
[self dismissViewControllerAnimated:YES completion:{
UIViewController *first = [UIViewController alloc] init];
[first.collectionView setContentOffset:appDelegate.tempContentValue animated:YES];
}];
#property (nonatomic,assign) CGPoint newContentOffset;
Then when you navigate to another View / present another view
self.newContentOffset = self.collectionView.contentOffset;
In viewWillAppear
[self.collectionView reloadData];
if (self.newContentOffset) {
[self performSelector:#selector(setContentOffset) withObject:nil afterDelay:0.1];
}
-(void)setContentOffset {
[self.collectionView setContentOffset:self.newContentOffset];
}
There will be a small jerk while applying this . But if thats ok, you can go ahead with this method
I can't find this anywhere. So, if you possess the info about it, please give me a link.
I have one view controller, I made a menu for a simple game. There are some buttons on it.
I have another view controller and there some buttons too.
The question is:
"How I can do an animation of this buttons (hiding off the screen) after I choose one button that triggers a custom segue (without any animation) to another View Controller, which will run it's button animation(coming to the screen from a border of the screen)?"
I made this like this:
1) Theory: I make a IBAction for a menu button, then in this IBAction I call an animation method, which call a performSegueMethod:. After this in new VC in viewWillAppear method call a animation method (that almost equal method from source VC). All this works, but this don't look smooth. The problem with this animation occurs when destination VC replace source VC. There is some split second, when all looks static, and only after this animation starts.
I don't know how to remove this destination VC lag. May be I must load a destination view before a segue? I tried to do this, but may be a made something wrong, or it's just don't help me.
2) Practice:
firstViewController.m:
- (IBAction)newGameSegueButton {
[self menuSlideInDirection:#"CenterToLeft" performingSegueWithIdentifier:#"mode"];
}
-(void)menuSlideInDirection:(NSString *)direction performingSegueWithIdentifier:(NSString *)segueIdentifier
{
[UIView animateWithDuration:0.4 animations:^{
CGPoint newGameButtonCenter;
newGameButtonCenter.x = directionIndex * 160;
newGameButtonCenter.y = self.gameButtonSlide.center.y;
self.gameButtonSlide.center = newGameButtonCenter;
[UIView animateWithDuration:0.3 delay:0.1 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
//some animation too
} completion:nil];
[UIView animateWithDuration:0.2 delay:0.2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
//animation
} completion:nil];
[UIView animateWithDuration:0.1 delay:0.3 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
//some animation too
} completion:^(BOOL finished){
if(segueIdentifier){
[self performSegueWithIdentifier:segueIdentifier sender:self];
}
}];
}];
}
Okay, then custom segue code is pretty simple:
-(void)perform
{
UIViewController *src = self.sourceViewController;
UIViewController *dst = self.destinationViewController;
[src.navigationController pushViewController:dst animated:NO];
}
And my secondViewController.m:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self menuSlideInDirection:#"RightToCenter" performingSegueWithIdentifier:nil];
}
menuSlideInDirection:#"RightToCenter" performingSegueWithIdentifier:nil in secondViewController identical to method with same name in firstView.
I'm looking for smooth animation of particular objects of destination view controller right after a segue.
May be a doing all wrong and there a another way to do this? (I only think of adding all view and all controls of destination VC to source VC and remove "destination VC" at all).
Hope, somebody can help me with this.
Try doing all first view controller's animations in perform method.
As for the second view controller's animations, I don't think there is any other 'good' way than doing it in viewWillAppear (although I would prefer viewDidAppear) since the outlets of the destination view controller are not set while performing the segue(they will be nil). In other words, you do not have a way to access your buttons, let alone animate them.
A hackish way would be to call [segue.destinationViewController view] before perform so that the destination view controller's view hierarchy is loaded and the outlets are set. Then perhaps, you may animate buttons in the destination view controller in perform before pushing it onto navigation stack.
For my question, I choose the way of putting two views under the same VC. Animation is smooth and it's look much better than using perform / viewWillAppear method.
I've a bunch of turn based games in my app, and I use the same animations to declare the starting player.
At the very end of viewDidLoad, I placed the code for declaration. It takes the screenshot of current view then blurs it a little, and labels appear to show the name of the starting player. The issue is sometimes it happens to fast that I got the screenshot of previous view and labels appear on the blurred screenshot of previous view.
My viewDidLoad looks like this:
-(void) viewDidLoad
{
[super viewDidLoad];
[self initializeThings];
[self layoutUI]; //In some of the games this part requires heavy processing,
//ie laying out a 2D array of buttons (20x20=400 of them)
[self showStartingPlayer];
}
I use the default transition style cover vertical in all VCs. I tried calling [self showStartingPlayer]; deferred by using performSelector with delay but different devices require different delay values so it is not a robust solution. Is there any other method I can use in viewcontroller lifecycle instead of viewDidLoad or any practical way of doing such a thing?
if you are using presentViewController: animated: completion:, i would take advantage of the completion block to notify the view controller that the transition is complete.
for example, you could add a public method called -(void)wasJustPresented to your view controller which calls the necessary UI layout.
Then, call this in your completion block. Ex:
[self presentViewController:newVC
animated:YES
completion:^(void){
[newVC wasJustPresented];
}];
This will ensure your view controller is notified right after it is done being presented.
viewDidLoad is called when the view of the view controller has been loaded, but it doesn't mean that it's actually visible on the screen.
You may use - (void) viewDidAppear: to do that.
You should try to call your method inside the viewDidAppear which is called as the view transition has finished.
-(void)viewDidAppear:animated
{
[super viewDidAppear:animated];
//put your call here
}
I need to load some data into a view every time it is shown. The data changes, each time time view is shown, so I figure I can load the data in the method viewDidAppear. Unfortunately, I've found that viewDidAppear is not called each time the view is displayed.
The code that displays the view from any other view is....
[self clearView];
[self.view insertSubview:fifthViewController.view atIndex:4];
So I figured I could change it to the following to run viewDidAppear...
[[self.view insertSubview:fifthViewController.view atIndex: 4 viewDidAppear:YES];
Unfortunaely, this causes an error "bad receiver type 'void'
What do I need to do to insert the subview and also call viewDidAppear?
If you show the view by modifying ViewController.view visibility directly, you won't get viewDidAppear message by that. You need to use ViewController method to display the view, e.g. push controller into UINavigationController or using presentModalViewController method. You can use the hack like calling viewWillAppear: and viewDidAppear: manually, but I don't like the idea.
Thank you for the assistance with this question.
I have settled on the addition of a viewDidAppear in the method that inserts the subview.
Below is the code that is working for me at this time.
In the .m file of the highest level view controller, the following code sets up the viewDidAppear call and then inserts the fifth subview.
-(IBAction) loadFifthView:(id)sender
{
[fifthViewController viewDidAppear:YES]; // sets up viewDidAppear
[self clearView];
[self.view insertSubview:fifthViewController.view atIndex:4];
}
With the above code snippet in place, the following code snippet, located in the .m file of the fifth view controller reports that it is working.
- (void)viewDidAppear:(BOOL)animated
{
NSLog(#" xxxxxxxxxxxxxxxx inside viewDidAppear ");
}