ios - UIView 过渡不符合预期

标签 ios

我正在使用以下代码为 View 设置动画。我希望 View 从顶部落到屏幕中央。

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

此代码有效,但 View 必须从顶部的中心点开始,但它从顶部的左侧点开始。为什么会这样?

最佳答案

在你的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

关于ios - UIView 过渡不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40815376/

相关文章:

ios - 如何在 swift 中使用 dbAccess 的特定字段更新多条记录

ios - 应用程序在后台保存几分钟后就会被杀死

ios - 是否可以创建一个仅显示网站的 webview 的 ios 应用程序?

ios - 如何通过 UIButton 变量调用操作

iOS 未找到有效的签名身份

ios - iOS 应用程序可以稍后添加到仅限 Apple Watch 的应用程序中吗?

iphone - Google 路线导航 API (iPhone)

ios - Fabric Crashlytics 如何使用 Xcode 集成插件在 Jenkins 中上传 dSYM 文件

iphone - iOS 错误 : No visible @interface for 'Project' declares the selector 'alloc'

ios - Swift google firebase VisionTextDetector 显示使用未声明的类型