我试图为注解视图的子视图的一些子视图设置动画。UIView块动画不从当前状态开始
所以,更清楚的是,灰色的针是我的注释视图,它有一个不可见的子视图,这个子视图包含这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
。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView
{
// in case the typeSelectionView was already being showed by another annotationView
if (self.typeSelectionView.superview != annotationView) {
[self.typeSelectionView removeFromSuperview];
[self.typeSelectionView showTypeButtons:NO animated:NO completion:NULL];
}
[annotationView addSubview:self.typeSelectionView];
[self.typeSelectionView showTypeButtons:YES animated:YES completion:NULL];
}
- (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
{
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:UIViewAnimationOptionBeginFromCurrentState
animations:block
completion:^(BOOL finished) {
if (completion) {
completion(finished);
}
}];
} else {
block();
if (completion) {
completion(YES);
}
}
}
到目前为止,没有问题,但是当一个灰色的引脚显示typeSelectionView(如上面的图片),然后我选择另一脚,是这样的部分:
if (self.typeSelectionView.superview != annotationView) {
[self.typeSelectionView removeFromSuperview];
[self.typeSelectionView showTypeButtons:NO animated:NO completion:NULL];
}
什么也没做。 4圈简单地从一个针跳到另一个没有任何动画。 我期望的是[self.typeSelectionView showTypeButtons:NO animated:NO completion:NULL];
将这些按钮设置为“隐藏”(比例尺(0,0)并且居中在引脚的顶部)而没有任何动画(因为不可见视图不再有超视图了,所以他们甚至没有显示),然后当我将typeSelectionView添加到新的注释视图[annotationView addSubview:self.typeSelectionView];
时,我使用[self.typeSelectionView showTypeButtons:YES animated:YES completion:NULL];
再次将圆圈设置为原始位置。
顺便说一句,做[typeButton.layer removeAllAnimations]
在showTypeButtons:animated:completion:
开头什么都不做。
我该如何解决这个问题?
在此先感谢。
编辑:(BONUS如果有人可以解释给我...帮助我解决这个问题)=)
假设圆圈上面显示类似的图片,那么这段代码:
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
所以第二动画完成第一和两个动画与完成。 第二个动画(首先完成)在动画时间结束后立即结束。
O.o
我得到了什么是你的动画,但无法理解你的问题.. –
我的问题是,当圆圈显示在一个针的顶部,然后我点击另一个脚,圆瞬间从第一脚消失,出现在第二个引脚上,没有任何动画。 –
我也很感激,如果你可以看看EDIT =) –