1

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


    }
}
4

1 回答 1

14

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

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

阅读您的代码有点令人困惑,所以我不能 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];

不过,我会努力使代码更具可读性。可能将东西移到不同的功能以减少代码复制等......

目前读起来很混乱。

于 2013-08-21T17:03:45.183 回答