砌体animationWithDuration没有动画

jac*_*cob 3 animation objective-c ios autolayout masonry-ios-osx

我试图从我的viewController顶部底部动画UIWebView,我正在使用砌体(https://github.com/Masonry/Masonry).

我最初创建了我的webview,其大小为0 - (x,y,height和width),然后我尝试对其进行动画处理,以便webview为viewcontroller的"on top"设置动画.显示了webview,但它没有动画到位 - 它只是立即出现.任何有经验的人都可以指导我朝着正确的方向前进吗?

这是我的按钮动作

-(void)didPressBtn{
    self.infoView = [UIWebView new];
    self.infoView.scalesPageToFit = YES;
    self.infoView.backgroundColor = [UIColor whiteColor];
    self.infoView.delegate = self;
    [self.scrollView addSubview:self.infoView];
    [self.infoView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];


    [self.scrollView layoutIfNeeded];
    //FIXME: Hmmm it doesn't really animate!?
    [UIView animateWithDuration:1 animations:^{
        [self.scrollView setContentOffset:CGPointMake(0, 0)];
        [self.infoView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.scrollView);
        }];

        [self.scrollView layoutIfNeeded];

    } completion:^(BOOL finished) {
                [self.infoView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:NSLocalizedString(@"INFORMATION_URL", )] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15]];
    }];
}
Run Code Online (Sandbox Code Playgroud)

我的viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.scrollView = [UIScrollView new];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    UIView *contentView = [UIView new];
    [self.scrollView addSubview:contentView];

    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];

//Then adding buttons and such...
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*ard 17

一个快速观察didPressButton.您正在使用mas_makeConstraints设置边等于@0和它们之后立即使用mas_makeConstraints再次更改它们以便滚动视图填充.这在第一组之上增加了矛盾的约束.

相反,您可以使用mas_remakeConstraints替换该视图上的现有约束.

至于动画,我使用的模式是首先更新约束(不在动画块内),然后为布局设置动画:

[UIView animateWithDuration:1
        animations:^{
            [theParentView layoutIfNeeded];
        }];
Run Code Online (Sandbox Code Playgroud)

另请参见如何设置约束更改的动画?

  • 谢谢!我总是忘记应该在**parentView**上调用`layoutIfNeeded`,而不是在动画视图本身上调用 (3认同)
  • 我又遇到了这个问题,用谷歌搜索,来到这里,意识到我在子视图上调用“layoutIfNeeded”,想对这个答案进行投票,但发现我已经对它进行了投票。然后我想至少对这条评论投赞成票,但后来意识到这是“我的”三年前的评论。哎呀,人类真是健忘啊…… (2认同)