没有完成 block 的 iOS 同步动画

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

我有一个带有标准工具栏的视频播放器。向下滑动手势即可关闭工具栏。我还有一个 View (实际上是一个面板),可以直接出现在工具栏上方,也可以通过向下滑动手势将其关闭。当面板和工具栏都打开时,向下滑动手势应关闭面板,再向下滑动手势应关闭工具栏。问题是,当滑动手势连续快速发生时(在面板动画完成之前),工具栏动画会抖动。

- (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 动画仍在运行。在模拟器中以慢动作执行双击时,第一个动画看起来不错,但工具栏动画很抖动。

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

有没有办法让多个动画 block 同时运行而不需要放置完成 block ?如果需要任何说明,请告诉我!谢谢!

最佳答案

我想知道问题是否与自动布局有关(在自动布局打开时设置框架会导致问题)。我尝试了一个简单的测试,通过动画化它们的约束常量来动画化屏幕底部的 View 和工具栏,动画看起来很好。我将 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];
    }
}

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

关于没有完成 block 的 iOS 同步动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20413281/

相关文章:

ios - 如何在半 View 上实现滑动手势,在另一半 View 上实现平移手势?

ios - 32 位的应用程序可以在 64 位的 iPhone 5S 上运行吗?

ios - 检索具有读取属性的文件并对其进行分类

ios - 在允许运行循环继续的同时延迟的正确方法

objective-c - 使用 stringWithUTF8String 和 dataUsingEncoding 将 NSString 转换为 NSData 返回 nil

xcode - 自动布局:高度不可变,尽管 "height >= constraint"

iphone - 我可以有一个 UICollectionViewFlowLayout 与布局方向不同的 scrollDirection 吗?

ios - 如何判断 NSManagedObject 是否被保存?

ios - 使用 HitTest :withEvent: to return custom object

objective-c - 在 Objective-C 中处理大数字?