简体   繁体   中英

how to make animation before a segue

I looked for a few articles on SO and on other sources, but can't find answer. This question will be a complex one. First, what i have: two view controllers (let's call them VC1 and VC2), and two buttons for one on each VC (let's call them in the same way as B1 and B2).

Here is image 在此处输入图片说明

When app start, B1 slide from right border of screen to middle position like on screenshot; I'll add code of this below. I want make this: when I click on B1, B1 slide left behind the border of the screen, segue is performed and then B2 button slide from right to centre(like B! button does). How can I make this? I must do this in custom segue class, or in some UIView method?

And on more question, if we click B1 (it slides beyond the left border), we at the VC2. If I click back button (bottom button on screenshot), I'll see white screen (because B1 have center coordinates about - 100). How i can prevent this too?

VC1 code

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *buttonOne;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGPoint startPossitionForButton = CGPointMake(345, 220);
    self.buttonOne.center = startPossitionForButton;


    [self slideButton];

}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [self slideButtonOffScreen];
}





-(void)slideButton
{
    [UIView animateWithDuration:0.3 animations:^{

    CGPoint endPossitionForButton = CGPointMake(155, 220);

    self.buttonOne.center = endPossitionForButton;

}];


}


-(void)slideButtonOffScreen
{
    [UIView animateWithDuration:0.3 animations:^{

        CGPoint endPossitionForButton = CGPointMake(-100, 220);

        self.buttonOne.center = endPossitionForButton;

    }];


}

VC2 code

@interface VC2 ()
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet UIButton *buttonTwo;

@end

@implementation VC2


- (IBAction)back:(id)sender {

    [self.navigationController popViewControllerAnimated:NO];


}



-(void)slideButton
{
    [UIView animateWithDuration:0.3 animations:^{

        CGPoint endPossitionForButton = CGPointMake(155, 220);

        self.buttonTwo.center = endPossitionForButton;

    }];


}


- (void)viewDidLoad
{
    [super viewDidLoad];


    CGPoint startPossitionForButton = CGPointMake(345, 220);
    self.buttonTwo.center = startPossitionForButton;


    [self slideButton];


}

Custom Segue class:

-(void)perform
{

    UIViewController *src = (UIViewController *) self.sourceViewController;
    UIViewController *dst = (UIViewController *) self.destinationViewController;

    [src.navigationController pushViewController:dst animated:NO];


}

Any help will be useful

Instead of calling the segue when pressing B1 call a helper action method. Inside this method perform the sliding and call the segue using performSegueWithIdentifier. To slide the button back check its position in viewWillAppear and move it back if necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM