ios - 砌体 animationWithDuration 没有动画

标签 ios objective-c animation autolayout masonry-ios-osx

我正在尝试从我的 viewController 顶部的底部为 UIWebView 设置动画,并且我正在使用砌体 ( https://github.com/Masonry/Masonry)。

我最初创建大小为 0 - (x,y,height and width) 的 webview,然后尝试为其设置动画,以便 webview 在 vi​​ewcontroller 的“顶部”设置动画。显示了 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]];
    }];
}

我的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...
}

最佳答案

关于 didPressButton 的快速观察。您正在使用 mas_makeConstraints 将边设置为等于 @0,然后立即再次使用 mas_makeConstraints 更改它们,以便填充 ScrollView 。这在第一组之上添加了相互矛盾的约束。

相反,您可以使用 mas_remakeConstraints 替换该 View 上的现有约束。

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

[UIView animateWithDuration:1
        animations:^{
            [theParentView layoutIfNeeded];
        }];

另见 How do I animate constraint changes?

关于ios - 砌体 animationWithDuration 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26722357/

相关文章:

iPhone View Controller 注册

ios - VFR Reader检测页面何时呈现

ios - 使用延迟动画暂停 CALayer 动画

ios - Instagram InstagramCaption 不工作

ios - popToViewController 不工作 "Tried to pop to a view controller that doesn' t 存在”

javascript - 当我改变位置时停止改变宽度

ios - 更好的是,委托(delegate)或单例在 View Controller 之间传递数据 - Objective-C

objective-c - 如何控制表格单元格文本标签的位置?

css - 使用第 nth-child 作为 CSS 变量

java - 如何用动画平滑填充进度条