【问题标题】:Autolayout Animation issue自动布局动画问题
【发布时间】:2015-04-29 06:05:46
【问题描述】:

我有 3 个视图层次结构(弹出框、页面容器、页面视图),页面视图放置在页面容器内,页面容器放置在弹出框内。它们都属于 UIView 类。

我在页面容器上添加了滑动手势。当发生滑动时,页面视图将替换为另一个页面视图。我试图在刷卡后立即获得动画。它从向左滑动的当前状态开始,但是当我向右滑动时它会混乱,并且在所有滑动之后它继续保持混乱。由于某种原因,动画不一致。

下面是代码,我尝试将约束放在动画块中,它没有任何区别。

- (IBAction)swipeLeft:(id)sender {

    if (_currentPage < [_pages count] - 1) {
        UIView *currentView = _pages[_currentPage];
        UIView *pageContainer = [currentView superview];

        [currentView removeFromSuperview];
        ++_currentPage;
        if (_pageControl)
            _pageControl.currentPage = _currentPage;
        UIView *newPage = _pages[_currentPage];


        [pageContainer addSubview:newPage];


        newPage.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
        newPage.frame = CGRectIntegral(newPage.frame);

        [pageContainer.superview layoutIfNeeded];

        NSDictionary *pageviews = NSDictionaryOfVariableBindings(newPage);

        if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
            [pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10.0-[newPage]-10.0-|" options:0 metrics:nil views:pageviews]];

        else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
            [pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];


        [pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];

        CGSize pageContainerSize = [pageContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);


        [UIView animateWithDuration:5.0
                              delay:0.0
                            options: UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             [pageContainer.superview layoutIfNeeded];

                         }
                         completion:^(BOOL finished){
                         }];

        UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];

        [self adjustPopoverOrientationWithCurrentOrientation:window];


    }
}

还有……

- (IBAction)swipeRight:(id)sender  {
    if (_currentPage > 0) {
        UIView *currentView = _pages[_currentPage];
        UIView *pageContainer = [currentView superview];

        [currentView removeFromSuperview];
        --_currentPage;
        if (_pageControl)
            _pageControl.currentPage = _currentPage;
        UIView *newPage = _pages[_currentPage];

        [pageContainer addSubview:newPage];

        newPage.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
        newPage.frame = CGRectIntegral(newPage.frame);

        [pageContainer.superview layoutIfNeeded];

        NSDictionary *pageviews = NSDictionaryOfVariableBindings(newPage);

        if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
            [pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10.0-[newPage]-10.0-|" options:0 metrics:nil views:pageviews]];

        else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
            [pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];

        [pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];

        CGSize pageContainerSize = [pageContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

        pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);



        [UIView animateWithDuration:5.0
                              delay:0.0
                            options: UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             [pageContainer.superview layoutIfNeeded];

                         }
                         completion:^(BOOL finished){
                         }];


        UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];


        [self adjustPopoverOrientationWithCurrentOrientation:window];


    }
}

【问题讨论】:

    标签: ios animation autolayout


    【解决方案1】:

    自动布局的第一条规则是你不能触摸框架。

    您不能更改自动布局层次结构中视图的框架、边界或中心。

    阅读您的代码有点令人困惑,所以我不能 100% 确定您实际上在制作什么动画。但是您需要做的是删除所有行,例如...

    pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);
    

    当您想在屏幕上移动某些东西时,您需要更新约束,以便新的约束暗示新的布局。

    例如,如果您想为视图的高度设置动画,那么您需要...

    保留对自动布局约束的引用...

    @property (nonatomic, strong) NSLayoutConstraint *heightConstraint;
    

    或者...

    // if you're creating the constraint in Interface Builder then CTRL drag it like any other outlet.
    @property (nonatomic, weak) IBOutlet NSLayoutConstraint *heightConstraint;
    

    然后更改约束的constant 属性。

    // animate the height to 150.
    heightConstraint.constant = 150;
    

    并为布局更改设置动画...

    [UIView animateWithDuration:5.0
                          delay:0.0
                        options: UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [pageContainer.superview layoutIfNeeded];
                     }
                     completion:^(BOOL finished){
                     }];
    

    一旦您知道哪些约束需要更改,您就可以确定如何最好地构建您的约束,以便它们可以动画化。

    编辑

    您还需要确保关闭自动调整大小蒙版翻译...

    [someView setTranslatesAutoresizingMaskIntoConstraints:NO];
    

    不过,我会努力使代码更具可读性。可能将内容移到不同的函数中以减少代码复制等...

    现在读起来很混乱。

    【讨论】:

    • 我添加了 CGSize pageContainerSize = [pageContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);
    • 对不起,我已经添加了上面的代码来查找 intrisinc 内容大小并将其设置为 pagecontainer。没有它我看不到视图框架。是的,我已经为我的所有视图设置了 setTranslatesAutoresizingMaskToConstraints 属性。我不知道现在该怎么办。
    • setTranslatesAutoresizingMaskToConstraints 未找到。你的意思是setTranslatesAutoresizingMaskIntoConstraints
    猜你喜欢
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    相关资源
    最近更新 更多