Skip to content

Instantly share code, notes, and snippets.

@fvisticot
Created August 17, 2011 11:58

Revisions

  1. digdog revised this gist Nov 22, 2009. 1 changed file with 17 additions and 17 deletions.
    34 changes: 17 additions & 17 deletions DDAnnotationView.m
    Original file line number Diff line number Diff line change
    @@ -10,13 +10,13 @@ @implementation DDAnnotationView

    - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
    self.enabled = YES;
    self.canShowCallout = YES;
    self.multipleTouchEnabled = NO;
    self.animatesDrop = YES;
    }
    return self;
    if ((self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier])) {
    self.enabled = YES;
    self.canShowCallout = YES;
    self.multipleTouchEnabled = NO;
    self.animatesDrop = YES;
    }
    return self;
    }

    #pragma mark -
    @@ -26,7 +26,7 @@ - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    // The view is configured for single touches only.
    // The view is configured for single touches only.
    UITouch* aTouch = [touches anyObject];
    _startLocation = [aTouch locationInView:[self superview]];
    _originalCenter = self.center;
    @@ -40,20 +40,20 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint newLocation = [aTouch locationInView:[self superview]];
    CGPoint newCenter;

    // If the user's finger moved more than 5 pixels, begin the drag.
    // If the user's finger moved more than 5 pixels, begin the drag.
    if ((abs(newLocation.x - _startLocation.x) > 5.0) || (abs(newLocation.y - _startLocation.y) > 5.0)) {
    _isMoving = YES;
    }
    _isMoving = YES;
    }

    // If dragging has begun, adjust the position of the view.
    // If dragging has begun, adjust the position of the view.
    if (_mapView && _isMoving) {
    newCenter.x = _originalCenter.x + (newLocation.x - _startLocation.x);
    newCenter.y = _originalCenter.y + (newLocation.y - _startLocation.y);
    self.center = newCenter;
    } else {
    // Let the parent class handle it.
    // Let the parent class handle it.
    [super touchesMoved:touches withEvent:event];
    }
    }
    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    @@ -70,9 +70,9 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    _startLocation = CGPointZero;
    _originalCenter = CGPointZero;
    _isMoving = NO;
    } else {
    } else {
    [super touchesEnded:touches withEvent:event];
    }
    }
    }

    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    @@ -87,7 +87,7 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    _isMoving = NO;
    } else {
    [super touchesCancelled:touches withEvent:event];
    }
    }
    }

    @end
  2. digdog revised this gist Nov 22, 2009. 1 changed file with 21 additions and 38 deletions.
    59 changes: 21 additions & 38 deletions DDAnnotationView.m
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,11 @@
    #import "DDAnnotationView.h"
    #import "DDAnnotation.h"

    @interface DDAnnotationView ()
    @property (nonatomic, assign) BOOL isMoving;
    @property (nonatomic, assign) CGPoint startLocation;
    @property (nonatomic, assign) CGPoint originalCenter;
    @end


    #pragma mark -
    #pragma mark DDAnnotationView implementation

    @implementation DDAnnotationView

    @synthesize isMoving = _isMoving;
    @synthesize startLocation = _startLocation;
    @synthesize originalCenter = _originalCenter;
    @synthesize mapView = _mapView;

    - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    @@ -24,14 +14,7 @@ - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString
    self.enabled = YES;
    self.canShowCallout = YES;
    self.multipleTouchEnabled = NO;
    self.animatesDrop = YES;

    UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"digdog.png"]];
    self.leftCalloutAccessoryView = leftIconView;
    [leftIconView release];

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    self.rightCalloutAccessoryView = rightButton;
    self.animatesDrop = YES;
    }
    return self;
    }
    @@ -45,8 +28,8 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    // The view is configured for single touches only.
    UITouch* aTouch = [touches anyObject];
    self.startLocation = [aTouch locationInView:[self superview]];
    self.originalCenter = self.center;
    _startLocation = [aTouch locationInView:[self superview]];
    _originalCenter = self.center;

    [super touchesBegan:touches withEvent:event];
    }
    @@ -58,14 +41,14 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint newCenter;

    // If the user's finger moved more than 5 pixels, begin the drag.
    if ((abs(newLocation.x - self.startLocation.x) > 5.0) || (abs(newLocation.y - self.startLocation.y) > 5.0)) {
    self.isMoving = YES;
    if ((abs(newLocation.x - _startLocation.x) > 5.0) || (abs(newLocation.y - _startLocation.y) > 5.0)) {
    _isMoving = YES;
    }

    // If dragging has begun, adjust the position of the view.
    if (self.isMoving) {
    newCenter.x = self.originalCenter.x + (newLocation.x - self.startLocation.x);
    newCenter.y = self.originalCenter.y + (newLocation.y - self.startLocation.y);
    if (_mapView && _isMoving) {
    newCenter.x = _originalCenter.x + (newLocation.x - _startLocation.x);
    newCenter.y = _originalCenter.y + (newLocation.y - _startLocation.y);
    self.center = newCenter;
    } else {
    // Let the parent class handle it.
    @@ -74,37 +57,37 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if (self.isMoving) {
    if (_mapView && _isMoving) {
    // Update the map coordinate to reflect the new position.
    CGPoint newCenter = self.center;
    DDAnnotation* theAnnotation = (DDAnnotation *)self.annotation;
    CLLocationCoordinate2D newCoordinate = [self.mapView convertPoint:newCenter toCoordinateFromView:self.superview];
    CLLocationCoordinate2D newCoordinate = [_mapView convertPoint:newCenter toCoordinateFromView:self.superview];

    [theAnnotation changeCoordinate:newCoordinate];

    // Clean up the state information.
    self.startLocation = CGPointZero;
    self.originalCenter = CGPointZero;
    self.isMoving = NO;
    } else {
    _startLocation = CGPointZero;
    _originalCenter = CGPointZero;
    _isMoving = NO;
    } else {
    [super touchesEnded:touches withEvent:event];
    }
    }

    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

    if (self.isMoving) {
    if (_mapView && _isMoving) {
    // Move the view back to its starting point.
    self.center = self.originalCenter;
    self.center = _originalCenter;

    // Clean up the state information.
    self.startLocation = CGPointZero;
    self.originalCenter = CGPointZero;
    self.isMoving = NO;
    _startLocation = CGPointZero;
    _originalCenter = CGPointZero;
    _isMoving = NO;
    } else {
    [super touchesCancelled:touches withEvent:event];
    }
    }

    @end
    @end
  3. digdog revised this gist Aug 2, 2009. 1 changed file with 27 additions and 17 deletions.
    44 changes: 27 additions & 17 deletions DDAnnotationView.m
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,21 @@
    #import "DDAnnotationView.h"
    #import "DDAnnotation.h"

    @interface DDAnnotationView ()
    @property (nonatomic, assign) BOOL isMoving;
    @property (nonatomic, assign) CGPoint startLocation;
    @property (nonatomic, assign) CGPoint originalCenter;
    @end


    #pragma mark -
    #pragma mark DDAnnotationView implementation

    @implementation DDAnnotationView

    @synthesize isMoving = _isMoving;
    @synthesize startLocation = _startLocation;
    @synthesize originalCenter = _originalCenter;
    @synthesize mapView = _mapView;

    - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    @@ -35,8 +45,8 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    // The view is configured for single touches only.
    UITouch* aTouch = [touches anyObject];
    _startLocation = [aTouch locationInView:[self superview]];
    _originalCenter = self.center;
    self.startLocation = [aTouch locationInView:[self superview]];
    self.originalCenter = self.center;

    [super touchesBegan:touches withEvent:event];
    }
    @@ -48,14 +58,14 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint newCenter;

    // If the user's finger moved more than 5 pixels, begin the drag.
    if ((abs(newLocation.x - _startLocation.x) > 5.0) || (abs(newLocation.y - _startLocation.y) > 5.0)) {
    _isMoving = YES;
    if ((abs(newLocation.x - self.startLocation.x) > 5.0) || (abs(newLocation.y - self.startLocation.y) > 5.0)) {
    self.isMoving = YES;
    }

    // If dragging has begun, adjust the position of the view.
    if (_isMoving) {
    newCenter.x = _originalCenter.x + (newLocation.x - _startLocation.x);
    newCenter.y = _originalCenter.y + (newLocation.y - _startLocation.y);
    if (self.isMoving) {
    newCenter.x = self.originalCenter.x + (newLocation.x - self.startLocation.x);
    newCenter.y = self.originalCenter.y + (newLocation.y - self.startLocation.y);
    self.center = newCenter;
    } else {
    // Let the parent class handle it.
    @@ -65,33 +75,33 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if (_isMoving) {
    if (self.isMoving) {
    // Update the map coordinate to reflect the new position.
    CGPoint newCenter = self.center;
    DDAnnotation* theAnnotation = (DDAnnotation *)self.annotation;
    CLLocationCoordinate2D newCoordinate = [_mapView convertPoint:newCenter toCoordinateFromView:self.superview];
    CLLocationCoordinate2D newCoordinate = [self.mapView convertPoint:newCenter toCoordinateFromView:self.superview];

    [theAnnotation changeCoordinate:newCoordinate];

    // Clean up the state information.
    _startLocation = CGPointZero;
    _originalCenter = CGPointZero;
    _isMoving = NO;
    self.startLocation = CGPointZero;
    self.originalCenter = CGPointZero;
    self.isMoving = NO;
    } else {
    [super touchesEnded:touches withEvent:event];
    }
    }

    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

    if (_isMoving) {
    if (self.isMoving) {
    // Move the view back to its starting point.
    self.center = _originalCenter;
    self.center = self.originalCenter;

    // Clean up the state information.
    _startLocation = CGPointZero;
    _originalCenter = CGPointZero;
    _isMoving = NO;
    self.startLocation = CGPointZero;
    self.originalCenter = CGPointZero;
    self.isMoving = NO;
    } else {
    [super touchesCancelled:touches withEvent:event];
    }
  4. digdog created this gist Jul 25, 2009.
    100 changes: 100 additions & 0 deletions DDAnnotationView.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    #import "DDAnnotationView.h"
    #import "DDAnnotation.h"

    #pragma mark -
    #pragma mark DDAnnotationView implementation

    @implementation DDAnnotationView

    @synthesize mapView = _mapView;

    - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
    self.enabled = YES;
    self.canShowCallout = YES;
    self.multipleTouchEnabled = NO;
    self.animatesDrop = YES;

    UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"digdog.png"]];
    self.leftCalloutAccessoryView = leftIconView;
    [leftIconView release];

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    self.rightCalloutAccessoryView = rightButton;
    }
    return self;
    }

    #pragma mark -
    #pragma mark Handling events

    // Reference: iPhone Application Programming Guide > Device Support > Displaying Maps and Annotations > Displaying Annotations > Handling Events in an Annotation View

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    // The view is configured for single touches only.
    UITouch* aTouch = [touches anyObject];
    _startLocation = [aTouch locationInView:[self superview]];
    _originalCenter = self.center;

    [super touchesBegan:touches withEvent:event];
    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch* aTouch = [touches anyObject];
    CGPoint newLocation = [aTouch locationInView:[self superview]];
    CGPoint newCenter;

    // If the user's finger moved more than 5 pixels, begin the drag.
    if ((abs(newLocation.x - _startLocation.x) > 5.0) || (abs(newLocation.y - _startLocation.y) > 5.0)) {
    _isMoving = YES;
    }

    // If dragging has begun, adjust the position of the view.
    if (_isMoving) {
    newCenter.x = _originalCenter.x + (newLocation.x - _startLocation.x);
    newCenter.y = _originalCenter.y + (newLocation.y - _startLocation.y);
    self.center = newCenter;
    } else {
    // Let the parent class handle it.
    [super touchesMoved:touches withEvent:event];
    }
    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if (_isMoving) {
    // Update the map coordinate to reflect the new position.
    CGPoint newCenter = self.center;
    DDAnnotation* theAnnotation = (DDAnnotation *)self.annotation;
    CLLocationCoordinate2D newCoordinate = [_mapView convertPoint:newCenter toCoordinateFromView:self.superview];

    [theAnnotation changeCoordinate:newCoordinate];

    // Clean up the state information.
    _startLocation = CGPointZero;
    _originalCenter = CGPointZero;
    _isMoving = NO;
    } else {
    [super touchesEnded:touches withEvent:event];
    }
    }

    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

    if (_isMoving) {
    // Move the view back to its starting point.
    self.center = _originalCenter;

    // Clean up the state information.
    _startLocation = CGPointZero;
    _originalCenter = CGPointZero;
    _isMoving = NO;
    } else {
    [super touchesCancelled:touches withEvent:event];
    }
    }

    @end