【问题标题】:iOS Simultaneous animations without completion block没有完成块的iOS同步动画
【发布时间】:2013-12-05 23:53:08
【问题描述】:

我有一个带有标准工具栏的视频播放器。向下滑动手势会关闭工具栏。我还有一个视图(实际上是一个面板),它可以直接显示在工具栏上方,并且也可以通过向下滑动手势来消除。当面板和工具栏都打开时,一个向下滑动手势应该关闭面板,第二个手势将关闭工具栏。问题是当滑动手势快速连续发生时(在面板动画完成之前),工具栏动画会抖动。

- (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
    UISwipeGestureRecognizerDirection direction = [gestureRecognizer direction];

    if (direction == UISwipeGestureRecognizerDirectionDown) {
        if (![toolbar isHidden]) {
            // Only dismiss the bottom panel if it is open
            if (_selectedSegmentIndex != UISegmentedControlNoSegment) {
                _selectedSegmentIndex = UISegmentedControlNoSegment;
                [bottomPanelView dismissPanel];
            } else {
                CGRect tempRect = CGRectMake(0, self.view.frame.size.height, toolbar.frame.size.width, toolbar.frame.size.height);
                [UIView animateWithDuration:0.25f
                                 animations:^{
                                    // Move the toolbar off the screen.
                                    toolbar.frame = tempRect;
                                 }
                                 completion:^(BOOL finished) {
                                     [toolbar setHidden:YES];
                                 }];
            }
        }
    }
}

[bottomPanelView dismissPanel] 在一个单独的类中,不知道调用它的类。它有跟随动画...

[UIView animateWithDuration:self.panelAnimationDuration
                      delay:0.0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     // slideOutLocation is off the screen 
                     self.view.frame = slideOutLocation;
                 }
                 completion:^(BOOL finished) {
                     [self.view removeFromSuperview];
                     [self removeFromParentViewController];
                     self.panelActive = NO;
                 }];

所以基本上,当关闭工具栏的动画开始时,dismissPanel 动画仍在运行。在模拟器中以慢动作执行双击时,第一个动画看起来不错,但工具栏动画很抖动。

我知道如何在完成块中嵌套动画,但是这里不能这样做,因为关闭面板和工具栏并不总是想要的。此外,dismissPanel 代码在其他地方处理,并且不受工具栏的控制。

有没有办法让多个动画块同时运行而不放置完成块?让我知道是否需要任何澄清!谢谢!

【问题讨论】:

  • 我认为当你第二次滑动时你应该停止第一个动画。在这里查看答案:stackoverflow.com/questions/554997/cancel-a-uiview-animation。然后你可以启动另一个动画让它返回,它现在在哪里并不重要。
  • 我已经尝试使用removeAllAnimations 作为该帖子中提到的用户之一删除动画。即使我在面板视图上运行removeAllAnimations,它似乎也阻止了我的工具栏动画,因为工具栏在第二次滑动时消失了,而不是从屏幕上滑下来。

标签: ios objective-c cocoa core-animation objective-c-blocks


【解决方案1】:

我想知道问题是否与自动布局有关(在自动布局开启时设置框架会导致问题)。我尝试了一个简单的测试,通过为它们的约束常量设置动画来为屏幕底部的视图和工具栏设置动画,并且动画看起来很好。我将 IBOutlets 设置为它们各自的底部约束(称为 viewBottomCon 和 toolBarBottomCon)。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isFirstSwipe = YES;
}

-(IBAction)downSwipe:(UISwipeGestureRecognizer *)sender {
    if (self.isFirstSwipe) {

        self.viewBottomCon.constant = -52;
        self.isFirstSwipe = NO;
        [UIView animateWithDuration:5 animations:^{
            [self.view layoutIfNeeded];
        } completion:nil];

    }else if (!self.isFirstSwipe) {

        self.toolBarBottomCon.constant = -44;
        [UIView animateWithDuration:3 animations:^{
            [self.view layoutIfNeeded];
        } completion:nil];
    }
}

这是一个比你的设置更简单的设置,但我认为它也应该适用于你的情况。

【讨论】:

  • 你不知道吗,关闭自动布局解决了这个问题。我以前启用了自动布局以在工具栏上定位某些项目,但是关闭它一切都很好。我已经开始尝试使用您的方法来操纵约束,但是鉴于我已经在没有它们的情况下构建所有东西已有一段时间了,要切换到在所有东西上使用它们还有很多工作要做。也许我稍后需要专门对我们的 Auto Layout 进行重构......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
相关资源
最近更新 更多