热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS11安全区域与UIView动画

如何解决《iOS11安全区域与UIView动画》经验,您有什么比较好的解决方法?

请考虑以下内容:我创建了一个可重用的UIView子类,它具有一个背景,并在其子视图中列出了一些内容.作为负责任的开发人员,我根据其安全区域对齐了视图的子视图,以便在必要时自动插入其内容(例如在iPhone X上):

中心vs底部

优秀!当内部视图位于安全区域内时,内部视图是边对边的,当它从安全区域悬挂出来时,内部视图将其内容从底部插入.

现在让我们说最初视图应该在屏幕外,但是当用户点击我的应用程序中的某个地方时,我希望我的视图动画到屏幕的中心,它应该在第二次点击时动画显示.让我们像这样实现它:

@implementation ViewController {
    UIView* _view;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(animate:)]];
}

- (void)animate:(UITapGestureRecognizer*)sender {
    if (_view == nil) {
        _view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        _view.backgroundColor = UIColor.redColor;

        UIView* innerView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 90, 90)];
        innerView.translatesAutoresizingMaskIntoCOnstraints= NO;
        innerView.backgroundColor = UIColor.blueColor;

        [_view addSubview:innerView];
        [_view addConstraints:@[[_view.safeAreaLayoutGuide.topAnchor constraintEqualToAnchor:innerView.topAnchor constant:-5],
                                [_view.safeAreaLayoutGuide.rightAnchor constraintEqualToAnchor:innerView.rightAnchor constant:5],
                                [_view.safeAreaLayoutGuide.bottomAnchor constraintEqualToAnchor:innerView.bottomAnchor constant:5],
                                [_view.safeAreaLayoutGuide.leftAnchor constraintEqualToAnchor:innerView.leftAnchor constant:-5]]];

        _view.center = CGPointMake(self.view.bounds.size.width / 2,
                                   self.view.bounds.size.height + _view.bounds.size.height / 2);
        [self.view addSubview:_view];
        [UIView animateWithDuration:1 animations:^{
            _view.center = CGPointMake(self.view.bounds.size.width / 2,
                                       self.view.bounds.size.height / 2);
        }];
    } else {
        [UIView animateWithDuration:1 animations:^{
            _view.center = CGPointMake(self.view.bounds.size.width / 2,
                                       self.view.bounds.size.height + _view.bounds.size.height / 2);
        } completion:^(BOOL finished) {
            [_view removeFromSuperview];
            _view = nil;
        }];
    }
}

@end

所以,现在如果我们启动应用程序并点击屏幕,就会发生这种情况:

动画片

到现在为止还挺好.但是,当我们再次点击它时会发生这种情况:

动画出来

正如您所看到的,即使视图完全超出了超视图的范围,安全区域插入仍然会被设置为好像它位于超视图的底部.

我们可以通过调用[self.view layoutIfNeeded]视图动画的动画块来使效果不那么突然,但它并没有消除问题 - 底部空间将以动画方式增长而不是突然扩展:

更好,但仍然很糟糕

这对我来说仍然是不可接受的.我希望它滑出来,就像它滑入一样,所有侧面都有5pt填充.

有没有办法实现这一点,而不添加喜欢shouldAlignContentToSafeAreashouldAlignContentToBounds我的视图标志?

我只是想让它自动工作,但工作正常 ......


推荐阅读
author-avatar
mnuee
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有