2014-01-16 38 views
1

我有一个UIView当我打开视图控制器从顶部飞。我给自己定了UIView的Y约束至-200和当视图加载,下面的叫,一切工作正常:问题与UIView的自动布局动画

- (void)updateViewConstraints 
{ 
    [super updateViewConstraints]; 
    self.popUpViewYConstraint.constant = 37.0f; 
    self.closeButtonYConstraint.constant = 28.0f; 
    [self.popUpBaseView setNeedsUpdateConstraints]; 
    [self.closeButton setNeedsUpdateConstraints]; 

    [UIView animateWithDuration:0.25f animations:^{ 
     [self.popUpBaseView layoutIfNeeded]; 
     [self.closeButton layoutIfNeeded]; 
     [self.view layoutIfNeeded]; 
    }]; 
} 

但现在我有一个关闭按钮应该动画UIView回-200位置,然后从屏幕上移除视图控制器。但是这个动画没有发生。视图控制器正在被直接删除。这是我在做什么:

- (IBAction)closePressed:(id)sender 
{ 
    NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers]; 
    self.navigationController.viewControllers = navigationArray; 

    [UIView animateWithDuration:2.0f animations:^{ 
     self.popUpViewYConstraint.constant = -200.0f; 
     [self.popUpBaseView layoutIfNeeded]; 
    } completion:^(BOOL finished){ 
     [navigationArray removeObjectAtIndex: 1]; 
     [self.baseView removeFromSuperview]; 
     [self.view removeFromSuperview]; 
    }]; 
} 

我提到this链接。它似乎在为他们工作,但不适合我。 请帮忙。

+0

如果删除,消除意见的代码会发生什么? (即只让完成块变空白)。另外,试试这个...'animateWithDuration:延迟:选项:动画:completion',而不是你的选项使用一个... UIViewAnimationOptionBeginFromCurrentState – Fogmeister

+0

什么也没有发生。视图控制器只是删除没有动画。 – Anil

+0

你可以在动画使视图出现之前和之后放置一个屏幕截图。 – Fogmeister

回答

0

这条线:

self.popUpViewYConstraint.constant = -200.0f; 

如若动画块之前被调用。此外,我不确定您的意见的层次结构,但请确保您使用layoutIfNeeded调用来调用正确的视图。

+0

试过这样做。没有不同。还是行不通。就视图层次而言。我有self.view然后self.baseView然后self.popUpBaseView。 – Anil

+0

是的,你可以在动画块内部或之前调用它。它不会有所作为。 – Fogmeister

+0

@Fogmeister有什么我可以做的来创建这个动画? – Anil

0

这个怎么样

- (IBAction)closePressed:(id)sender 
{ 
    NSMutableArray *navigationArray = [[NSMutableArray alloc]:initWithArray:self.navigationController.viewControllers]; 
    self.navigationController.viewControllers = navigationArray; 

    // I am sure the constant should be set outside the animation block. It won't happen until next run loop, which will be inside the block. 
    self.popUpViewYConstraint.constant = -200.0f; 
    // setNeedsUpdateConstraints should be called on the view to which the constraint is added, not the view affected by the constraint. 
    [self.baseView setNeedsUpdateConstraints]; 

    [UIView animateWithDuration:2.0f animations:^{ 
     // I think this should be topmost view 
     [self.view layoutIfNeeded]; 
    } completion:^(BOOL finished){ 
     [navigationArray removeObjectAtIndex: 1]; 
     [self.baseView removeFromSuperview]; 
     [self.view removeFromSuperview]; 
    }]; 
} 
+0

我想我有一个不允许这种动画发生的约束。认为这是阻止这种情况发生的原因。 – Anil