Skip to content

Instantly share code, notes, and snippets.

@xuyunan
Last active December 11, 2015 21:28
Show Gist options
  • Save xuyunan/4662711 to your computer and use it in GitHub Desktop.
Save xuyunan/4662711 to your computer and use it in GitHub Desktop.
自定义annotation的动画
// 从上往下落的动作 (MKMapViewDelegate method)
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
CGRect visibleRect = [mapView annotationVisibleRect];
for (MKAnnotationView *view in views) {
CGRect endFrame = view.frame;
CGRect startFrame = endFrame;
startFrame.origin.y = visibleRect.origin.y - startFrame.size.height;
view.frame = startFrame;
[UIView beginAnimations:@"drop" context:NULL];
[UIView setAnimationDuration:0.5f];
view.frame = endFrame;
[UIView commitAnimations];
}
}
// 拖拽大头针时抬起动作 (在继承MKAnnotationView的子类中重新此方法)
- (void)setDragState:(MKAnnotationViewDragState)newDragState animated:(BOOL)animated
{
if (newDragState == MKAnnotationViewDragStateStarting)
{
CGPoint endPoint = CGPointMake(self.center.x,self.center.y-20);
[UIView animateWithDuration:0.2
animations:^{ self.center = endPoint; }
completion:^(BOOL finished)
{ self.dragState = MKAnnotationViewDragStateDragging; }];
}
else if (newDragState == MKAnnotationViewDragStateEnding)
{
CGPoint endPoint = CGPointMake(self.center.x,self.center.y+20);
[UIView animateWithDuration:0.2
animations:^{ self.center = endPoint; }
completion:^(BOOL finished)
{ self.dragState = MKAnnotationViewDragStateNone; }];
}
else if (newDragState == MKAnnotationViewDragStateCanceling)
{
CGPoint endPoint = CGPointMake(self.center.x,self.center.y+20);
[UIView animateWithDuration:0.2
animations:^{ self.center = endPoint; }
completion:^(BOOL finished)
{ self.dragState = MKAnnotationViewDragStateNone; }];
}
}
// http://stackoverflow.com/questions/3677166/subclassing-mkannotationview-and-overriding-setdragstate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment