Question

Today, I am trying to get a view to return to its default origin after a cancel. I am using two VCs to do this. One is a footer controller in a tableview, and the other is a modal view, which is presented after the first animation. Whenever I try to go back from the modal view, the origin is still the same one after I did the first animation. Here is the code I am using:

 Footer:

    -(IBAction)addPerson:(id)sender{
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.25];
        NSLog(@"%f", self.view.frame.origin.y);
      self.view.frame = CGRectMake(0,-368,320,400);
        [UIView commitAnimations];

        self.tdModal2 = [[TDSemiModalViewController2 alloc]init];


        //    [self.view addSubview:test.view];

        [self presentSemiModalViewController2:self.tdModal2];
}

-(void)moveBack{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    NSLog(@"%f", self.view.frame.origin.y);
    self.view.frame = CGRectMake(0,368,320,400);
    [UIView commitAnimations];
}

And in the modal view:

-(IBAction)cancel:(id)sender{
     [self dismissSemiModalViewController:self];
    FooterViewController *foot = [[FooterViewController alloc]init];
    self.footer = foot;
 //   self.footer.view.frame = CGRectMake(0,35,320,400);
    [self.footer moveBack];

}
Was it helpful?

Solution

I give the following recommendations, they may be good for you.

Note 1, AffineTransform

If the translation is always to the same point and always with the same measure, I'd recommend using CGAffineTransformMakeTranslation(<#CGFloat tx#>, <#CGFloat ty#>) instead of modifying the view's frame. This method specifies how much x and y points will the view move.

In that way, returning the view to the original position is as simple as doing view.transform = CGAffineTransformIdentity.

Both of these inside their respective animation block of course.

Note 2, Using CGPoint to move the origin

If you just move the origin of the view then the recommendation is to make:

CGRect hiddenFrame = self.view.frame;
hiddenFrame.origin.y -= 736;
self.view.frame = hiddenFrame;

or

CGRect hiddenFrame = self.view.frame;
hiddenFrame.origin.y = -368;
self.view.frame = hiddenFrame;

or

CGRect hiddenFrame = self.view.frame;
hiddenFrame.origin = CGPointMake(0,-368);
self.view.frame = hiddenFrame;

The same for the move back. It's more code but it's more understandable.

Note 3, UIView animation block

You should use the new blocks:

[UIView animateWithDuration: 0.25 animations: ^(void) {
        //animation block
}];

There are other blocks with more methods as delay, completion blocks, etc.

Option, Delegate or reference passing

When you create your modal controller, pass the reference of the current controller:

self.tdModal2 = [[TDSemiModalViewController2 alloc]init];
self.tdModal2.delegate = self;

You should declare that property in the TDSemiModalViewController2.h. Either by declaring the @class FooterViewController to avoid crossed imports; by making a protocol and declaring the property as id<myModalProtocol>, FooterViewController should then implement the protocol with the method moveBack; Or just declare the property as id and call [self.delegate performSelector: @selector(moveBack)].

Then in the cancel method, simply do:

[self dismissSemiModalViewController:self];
[self.delegate moveBack] //or performSelector.. in the third option case
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top