iOS - 使用自动布局执行多个同步动画

标签 ios ios7 ios8 autolayout ios-autolayout

我想同时执行两个运动动画。 firstView 上的第一个动画立即开始。 secondView 上的第二个动画在第一个动画仍在运行时略有延迟后开始。 secondView 约束与firstView 相关。该代码在 iOS 8 上运行良好。

firstViewsecondViewview 的 subview

view  
    |--- firstView  
    |--- secondView    

代码:

UIView *firstView = ...;
UIView *secondView = ...;    

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:firstView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self.view addConstraint:constraint];
    [self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];

[UIView animateWithDuration:0.5 delay:0.15 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:secondView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:firstView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self.view addConstraint:constraint];
    [self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];

在 iOS 7 上,一旦第二个 layoutIfNeeded 被调用,第一个动画停止,只有第二个动画动画。

有什么建议吗?

最佳答案

回答我自己的问题。

我最终用两步(三步,取决于你如何计算)解决方案来制作动画。首先在不调用 layoutIfNeeded 的情况下添加约束。然后更新 firtViewsecondView 框架。最后在最后一个动画的完成 block 中调用 layoutIfNeeded

代码:

UIView *firstView = ...;
UIView *secondView = ...;    

/* first step */
NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:firstView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
[self.view addConstraint:constraint1];
NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:secondView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:firstView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
[self.view addConstraint:constraint2];

/* second step */
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    CGRect firstViewFrame = CGRectMake(...); // set frame according to constaint1
    firstView.frame = firstViewFrame;
}];

[UIView animateWithDuration:0.5 delay:0.15 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    CGRect secondViewFrame = CGRectMake(...); // set frame according to constaint2
    secondView.frame = secondViewFrame;
} completion:^(BOOL finished) {
    /* second and a half step */
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
}];

关于iOS - 使用自动布局执行多个同步动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28557452/

相关文章:

ios - UNCalendarNotificationTrigger与开始日期

ios - IOS事件后是否可以触发本地通知?

ios - NSPredicate 通过选择带有 Beginswith 的键来获取 NSDictionary 的值

ios - iOS8 上的新固定位置错误

ios - Swift 设置变量内联还是在函数中?

ios - 有没有办法找到套接字对等进程的pid?

ios - 如何检查代码是否在 UIView 的动画 block 中运行

objective-c - xcode构建失败没有解释

ios - UINavigationItem 的背景颜色

ios8 - 如何禁用应用程序扩展?