Ios UIView块动画不是从当前状态开始的

Ios UIView块动画不是从当前状态开始的,ios,objective-c,animation,uiview,uiviewanimation,Ios,Objective C,Animation,Uiview,Uiviewanimation,我正在尝试为注释视图的子视图的一些子视图设置动画 所以,更清楚地说,灰色的pin是我的注释视图,它有一个不可见的子视图,这个子视图包含这4个彩色圆圈视图 annotation view | invisible view (called typeSelectionView) | yellow view (called typeButton) | blue view (called typeButton) | green view (called typeButton)

我正在尝试为注释视图的子视图的一些子视图设置动画

所以,更清楚地说,灰色的pin是我的注释视图,它有一个不可见的子视图,这个子视图包含这4个彩色圆圈视图

annotation view
 | invisible view (called typeSelectionView)
    | yellow view (called typeButton)
    | blue view (called typeButton)
    | green view (called typeButton)
    | red view (called typeButton)
首先,这些圆不可见,变换比例为0,0,它们的中心位于灰针的头部,然后,当我选择灰针时,这些圆从针头开始生长并移动到它们的原始位置,即上图中的位置

取消选择管脚时,会出现相反的动画。 当我选择和取消选择或相反的动画完成之前,它改变了它的方式,如果它正在增长它缩小恢复动画在中间。这就是为什么我需要UIViewAnimationOptionBeginFromCurrentState

什么也没做。 这4个圆在没有任何动画的情况下从一个引脚简单地跳到另一个引脚。 我所期望的是[self.typeSelectionView showTypeButtons:NO动画:NO completion:NULL];将这些按钮设置为隐藏比例0、0,并在引脚头部居中,不带任何动画,因为不可见视图不再具有superview,因此它们甚至不会显示,然后当我使用[annotationView addSubview:self.typeSelectionView]将typeSelectionView添加到新的annotationView时;,我使用[self.typeSelectionView showTypeButtons:YES animated:YES completion:NULL];再次将圆设置为其原始位置的动画

顺便说一句,在showTypeButtons:animated:completion:的开头执行[typeButton.layer removeAllAnimations]什么都不做

我如何解决这个问题

提前谢谢

编辑:如果有人能向我解释这一点,我会得到奖励。。。帮我解决这个问题=

假设圆圈如上图所示,则此代码:

NSLog(@"type selected");
[self.typeSelectionView showTypeButtons:NO
                               animated:YES
                             completion:^(BOOL finished) {
                                 NSLog(@"finisheddddd: %d", finished);
                             }];

[self.typeSelectionView showTypeButtons:NO
                               animated:YES
                             completion:^(BOOL finished) {
                                 NSLog(@"finished: %d", finished);
                             }];
生成以下输出:

type selected
finished: 1
finisheddddd: 1
因此,第二个动画首先完成,两个动画都以finished==YES结束。 另外,第一个动画完成的第二个动画立即完成,而不是在动画时间结束后完成


O.O

检查我编写但未测试的代码。请用它做实验以得到结果

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)annotationView
{
if (self.typeSelectionView.superview)
{
    [self.typeSelectionView showTypeButtons:NO
                                   animated:YES
                                 completion:^(BOOL finished) {
                                     if (finished) {
                                         [self.typeSelectionView removeFromSuperview];
                                     }
                                 }];
}
}

 - (void)showTypeButtons:(BOOL)show animated:(BOOL)animated completion:(void (^)  (BOOL))completion
 {

[UIView animateWithDuration:(animated) ?0.5f : 0.0f
                      delay:(self.animationStarted)? 0.5 : 0.0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     self.animationStarted = YES;
                     [self setButtonVisible:show];
                 }
                 completion:^(BOOL finished) {
                     self.animationStarted = NO;
                         if (completion) {
                             completion(finished);
                         }
                     }];
}

-(void)setButtonVisible:(BOOL)visible
{
 [self.typeButtons enumerateObjectsUsingBlock:^(TypeButton typeButton, NSUInteger idx, BOOL *stop) {
        if (visible)
         {
             typeButton.center = [self _typeButtonCenterForIndex:idx numberOfButtons:self.typeButtons.count];
             typeButton.transform = CGAffineTransformIdentity;
         }
         else
         {
             typeButton.center = CGPointMake(viewWidth / 2, viewheight + 5); // pin head center
             typeButton.transform = CGAffineTransformMakeScale(0, 0);
         }

 }];
}

问题是,当我将我的typeButtons设置为非动画时,它们不会立即重新绘制,因此它们不会更改其当前状态,因此发生的情况是:

one mapView:didSelectAnnotationView: method call:
 - show the buttons animated

***redraw cycle here***

another mapView:didSelectAnnotationView: method call:
- hide the buttons non-animated
(no redraw cycle here, so buttons are still showing)
- show the buttons animated
所以我想出了一个糟糕的解决方案:

- (void)showTypeButtons:(BOOL)show animated:(BOOL)animated completion:(void (^)(BOOL))completion
{
    void (^block)(void) = ^{
        for (int i = 0; i < self.typeButtons.count; i++) {
            TypeButton *typeButton = self.typeButtons[i];

            if (show) {
                typeButton.center = [self _typeButtonCenterForIndex:i numberOfButtons:self.typeButtons.count];
                typeButton.transform = CGAffineTransformIdentity;
            } else {
                typeButton.center = CGPointMake(viewWidth / 2, viewheight + 5); // pin head center
                typeButton.transform = CGAffineTransformMakeScale(0, 0);
            }
        }
    };

    if (animated) {
        [UIView animateWithDuration:0.5
                              delay:0
                            options:self.typeButtonsChangedWithoutAnimation ? 0 : UIViewAnimationOptionBeginFromCurrentState
                         animations:block
                         completion:^(BOOL finished) {
                             if (completion) {
                                 completion(finished);
                             }
                         }];
        self.typeButtonsChangedWithoutAnimation = NO;
    } else {
        self.typeButtonsChangedWithoutAnimation = YES;

        block();

        if (completion) {
            completion(YES);
        }
    }
}
如果没有UIViewAnimationOptionBeginFromCurrentState,动画将从其实际状态开始,而不是从其绘制状态开始

我找到了一个解决方案,但没有奏效: 即使它真的有用,我也不想要它

顺便说一句,我不会接受这个答案,因为它很烂。 它实际上并没有解决我的问题,即改变当前状态,而不是通过它。
我只是想发布它,以防有人看到并知道如何实际解决我的原始问题。

我知道你的动画是什么,但无法理解你的问题。我的问题是,当圆圈显示在一个管脚上,然后我点击另一个管脚,圆圈立即从第一个管脚消失,出现在第二个管脚上,没有任何动画。如果您可以在选择另一个管脚时查看EDIT=,我将不胜感激,它会调用取消选择方法吗?您是否更改了DidDecleAnnotationView方法中的任何内容?我尝试了您的代码,当已经选择了一个管脚并且选择了另一个管脚时,圆圈仍在跳跃。此外,我有时还注意到一些奇怪的延迟。谢谢你的帮助。
one mapView:didSelectAnnotationView: method call:
 - show the buttons animated

***redraw cycle here***

another mapView:didSelectAnnotationView: method call:
- hide the buttons non-animated
(no redraw cycle here, so buttons are still showing)
- show the buttons animated
- (void)showTypeButtons:(BOOL)show animated:(BOOL)animated completion:(void (^)(BOOL))completion
{
    void (^block)(void) = ^{
        for (int i = 0; i < self.typeButtons.count; i++) {
            TypeButton *typeButton = self.typeButtons[i];

            if (show) {
                typeButton.center = [self _typeButtonCenterForIndex:i numberOfButtons:self.typeButtons.count];
                typeButton.transform = CGAffineTransformIdentity;
            } else {
                typeButton.center = CGPointMake(viewWidth / 2, viewheight + 5); // pin head center
                typeButton.transform = CGAffineTransformMakeScale(0, 0);
            }
        }
    };

    if (animated) {
        [UIView animateWithDuration:0.5
                              delay:0
                            options:self.typeButtonsChangedWithoutAnimation ? 0 : UIViewAnimationOptionBeginFromCurrentState
                         animations:block
                         completion:^(BOOL finished) {
                             if (completion) {
                                 completion(finished);
                             }
                         }];
        self.typeButtonsChangedWithoutAnimation = NO;
    } else {
        self.typeButtonsChangedWithoutAnimation = YES;

        block();

        if (completion) {
            completion(YES);
        }
    }
}