UIView过渡并不像预期的那样

时间:2016-11-26 05:47:21

标签: ios

我使用以下代码为视图设置动画。我希望视图从屏幕的顶部落到中心。

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];

此代码有效,但视图必须来自顶部的中心点,但它来自顶部的左侧点。为什么这样?

2 个答案:

答案 0 :(得分:1)

尝试从顶部开始,

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

    CGRect newFrame = viewController.view.frame;
    newFrame.origin.y = -[UIScreen mainScreen].bounds.size.height;
    viewController.view.frame = newFrame;

    [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];

答案 1 :(得分:1)

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