user31231234124 user31231234124 - 12 days ago 7
iOS Question

UIView transition is not as expected

Im using the following code to animate view. Im expecting the view to fall from the top to the centre of the screen.

ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];

[UIView transitionWithView:viewController.view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^{
viewController.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2 , 18, 36);
}
completion:nil];

[viewController willMoveToParentViewController:self];
[self.view addSubview:viewController.view];
[self addChildViewController:viewController];
[viewController didMoveToParentViewController:self];


This code works but the view has to originate from the centre point of the top but it originates from the left point of the top . Why so?

Answer

In your vc1:

#import "ViewController.h"
#import "ViewController2.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

#pragma mark - init

- (void)initUI {

    self.view.backgroundColor = [UIColor whiteColor];



    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController2 *vc2 = [sb instantiateViewControllerWithIdentifier:@"ViewController2"];
    vc2.view.frame = CGRectMake(0, 0, 100, 100);
    vc2.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2.0, 0);

    [UIView transitionWithView:vc2.view
                  duration:0.5
                   options:UIViewAnimationOptionTransitionNone
                animations:^{
                    //vc2.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2 , 18, 36);
                    vc2.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2);
                }
                completion:nil];

    [vc2 willMoveToParentViewController:self];
    [self.view addSubview:vc2.view];
    [self addChildViewController:vc2];
    [vc2 didMoveToParentViewController:self];
}


@end