Why does UIView:animateWithDuration complete immediately?

ぐ巨炮叔叔 提交于 2019-12-11 00:32:52

问题


I have a subclass of MKPointAnnotation with the following method which I run on iOS 4:

- (void)animate {
    [UIView
     animateWithDuration: 3.0
     delay:1.0
     options:UIViewAnimationOptionAllowUserInteraction
     animations:^{
         CLLocationCoordinate2D loc = 
             CLLocationCoordinate2DMake([self coordinate].latitude + 0.001,
                                        [self coordinate].longitude + 0.001);
         [self setCoordinate:loc];

     }
     completion:^(BOOL finished) {
         NSLog(@"Is completed");
     }];
}

It is called by clicking a UIBarButtonItem.

I expect to see the my annotation travel across a MKMapView. However all I see is the annotation in its final resting place when I call the method like this:

[mapView addAnnotation:myAnnotation];
[myAnnotation animate];
[myAnnotation release];

The intended animation only occurs if I call the method like this:

[mapView addAnnotation:myAnnotation];
[myAnnotation performSelector:@selector(animate) withObject:nil afterDelay:0.5];
[myAnnotation release];

Note that I get the unintended behavior if the 'afterDelay', is smaller e.g. < 0.1s.

Any ideas why this could be the case?


回答1:


The animation ultimately acts on the MKAnnotationView, not the MKAnnotation. Adding an annotation to a MKMapView does not necessarily mean that the corresponding view will be added any time soon.

Therefore you get crazy behaviour if you attempt to animate a MKAnnotation before its corresponding MKAnnotationView has been added to the MKMapView.

The solution is to only animate a MKAnnotation once you know its corresponding MKAnnotationView has been added to MKView. You can know this by using MKMapViewDelegate - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views to trigger your animation.



来源:https://stackoverflow.com/questions/5869954/why-does-uiviewanimatewithduration-complete-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!