在代码块中添加和删除子视图不会动画

时间:2010-09-24 22:11:23

标签: iphone objective-c animation

我有这个代码用于在地图上作为MKAnnotationView的一部分闪烁图像:

    UIView* containerView = [mapView viewForAnnotation:enemy];
    UIImageView* redCircle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Red_half_circle.png"]];


    [UIView transitionWithView:containerView
                      duration:2.9
                       options:UIViewAnimationOptionAutoreverse&&UIViewAnimationOptionRepeat
                    animations:^{ [containerView addSubview:redCircle];}
                    completion:^(BOOL finished){[redCircle removeFromSuperview]; }];

因此,当我运行它时没有任何反应。但!!!如果我删除“完成”代码块并将其替换为NULL,那么至少动画会添加子视图。也许这些选项与它有关?

问题:如何在MKAnnotationView中闪现图像?

非常感谢。

1 个答案:

答案 0 :(得分:2)

首先要做的事情:

您已指定UIViewAnimationOptionAutoreverse&&UIViewAnimationOptionRepeat。这是“逻辑AND”,而不是“按位AND”。 Autoreverse为16(1<<4),Repeat为8(1<<3),因此Autoreverse&&Repeat16&&8,与{{1}相同(因为它们被评估为真值),恰好与1相同。

您想要UIViewAnimationOptionLayoutSubviews

第二:你没有指定过渡;这相当于指定UIViewAnimationOptionAutoreverse&UIViewAnimationOptionRepeat。我认为这意味着会忽略持续时间(因为您已经指定了UIViewAnimationOptionTransitionNone),因此您可以立即完成,因此您看不到任何内容。

第三:我也不认为...TransitionNone转换可以重复/反转(它需要跟踪你已删除/添加的视图;据我所知它只保留一个“截图“初始状态”,但我可能是错的。

你可能会有更好的运气:

UIView

这应该让它淡入淡出。您可能能够通过设置[containerView addSubview:redCircle]; redCircle.alpha = 0; [UIView animateWithDuration:2.9 delay:0 options:UIViewAnimationOptionAutoreverse&UIViewAnimationOptionRepeat animation:^{redCircle.alpha = 1;} completion:NULL]; 来使其闪烁(redCircle.hidden未记录为可动画,但UIView.hidden是可动画的,因此它可能仍然有效)