Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I didn't study Ben's code much but it didn't worked for me. So I tried Brian's and it works great. Thanks a lot! I've been trying to solve annotation's animation during drag'n'drop for a long time.</p> <p>But I have one suggestion to Brian's solution. I think that it would be better to support MKMapKit's delegate and notify it about changing dragState and save new position within standard delegate's method: <code>- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState</code>. Here's my code:</p> <p>DraggableAnnotationView.h:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;MapKit/MapKit.h&gt; @interface DraggableAnnotationView : MKAnnotationView { id &lt;MKMapViewDelegate&gt; delegate; MKAnnotationViewDragState dragState; } @property (nonatomic, assign) id &lt;MKMapViewDelegate&gt; delegate; @property (nonatomic, assign) MKAnnotationViewDragState dragState; @property (nonatomic, assign) MKMapView *mapView; @end </code></pre> <p>DraggableAnnotationView.m:</p> <pre><code>#import "DraggableAnnotationView.h" #import "MapAnnotation.h" @implementation DraggableAnnotationView @synthesize delegate, dragState, mapView; - (void)setDragState:(MKAnnotationViewDragState)newDragState animated:(BOOL)animated { [delegate mapView:mapView annotationView:self didChangeDragState:newDragState fromOldState:dragState]; if (newDragState == MKAnnotationViewDragStateStarting) { // lift the pin and set the state to dragging CGPoint endPoint = CGPointMake(self.center.x,self.center.y-20); [UIView animateWithDuration:0.2 animations:^{ self.center = endPoint; } completion:^(BOOL finished) { dragState = MKAnnotationViewDragStateDragging; }]; } else if (newDragState == MKAnnotationViewDragStateEnding) { // drop the pin, and set state to none CGPoint endPoint = CGPointMake(self.center.x,self.center.y+20); [UIView animateWithDuration:0.2 animations:^{ self.center = endPoint; } completion:^(BOOL finished) { dragState = MKAnnotationViewDragStateNone; }]; } else if (newDragState == MKAnnotationViewDragStateCanceling) { // drop the pin and set the state to none CGPoint endPoint = CGPointMake(self.center.x,self.center.y+20); [UIView animateWithDuration:0.2 animations:^{ self.center = endPoint; } completion:^(BOOL finished) { dragState = MKAnnotationViewDragStateNone; }]; } } - (void)dealloc { [super dealloc]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload