Hogyan tudok létrehozni egy egyedi „pin-drop” animáció segítségével MKAnnotationView?

szavazat
23

Van egy példánya MKMapView, és szeretné használni az egyéni kommentár ikonok helyett a szabványos csap ikonok által szolgáltatott MKPinAnnotationView. Így hát, beállít egy alosztálya MKAnnotationView nevű CustomMapAnnotation vagyok, és kényszerítő -(void)drawRect:kell felhívni a CGImage. Ez működik.

A baj akkor jön, amikor megpróbálom megismételni a .animatesDropfunkcionalitás által szolgáltatott MKPinAnnotationView; Szeretném én -ikonok fokozatosan csökkent felülről és balról jobbra haladva, amikor a kommentárokat adunk a MKMapViewpéldány.

Itt - (void) drawRect: az CustomMapAnnotation, ami akkor működik, ha csak felhívni a UIImage (ami a 2. sorban nem):

- (void)drawRect:(CGRect)rect {
 [super drawRect:rect];
 [((Incident *)self.annotation).smallIcon drawInRect:rect];
 if (newAnnotation) {
  [self animateDrop];
  newAnnotation = NO;
 }
} 

A baj akkor jön, amikor hozzá a animateDropmódszer:

-(void)animateDrop {
 CGContextRef myContext = UIGraphicsGetCurrentContext();

 CGPoint finalPos = self.center;
 CGPoint startPos = CGPointMake(self.center.x, self.center.y-480.0);
 self.layer.position = startPos;

 CABasicAnimation *theAnimation;
 theAnimation=[CABasicAnimation animationWithKeyPath:@position];
 theAnimation.fromValue=[NSValue valueWithCGPoint:startPos];
 theAnimation.toValue=[NSValue valueWithCGPoint:finalPos];
 theAnimation.removedOnCompletion = NO;
 theAnimation.fillMode = kCAFillModeForwards;
 theAnimation.delegate = self;
 theAnimation.beginTime = 5.0 * (self.center.x/320.0);
 theAnimation.duration = 1.0;
 [self.layer addAnimation:theAnimation forKey:@];
}

Ez egyszerűen nem működik, és nem lehet sok oka lehet, hogy miért. Nem fogok bejutni mindegyiket most. A legfontosabb dolog, én akartam tudni, ha a megközelítés hang egyáltalán, vagy ha kell próbálni valami teljesen más.

Próbáltam is, hogy a csomag az egészet egy animációs ügyletet úgy, hogy a beginTime paraméter esetleg valóban működik; ez úgy tűnt, hogy nem csinál semmit. Nem tudom, ha ez azért van, mert én hiányzik néhány fontos pont, vagy azért, mert MapKit van lomtárba én animációk valahogy.

  // Does nothing
  [CATransaction begin];
  [map addAnnotations:list];
  [CATransaction commit];

Ha valakinek van bármilyen tapasztalata animált MKMapAnnotations mint ez, szeretnék néhány tanácsot, egyébként ha tudsz ajánlani CAAnimation tanácsot a megközelítés, hogy jó lenne túl.

A kérdést 07/12/2009 01:38
a forrás felhasználó
Más nyelveken...                            


5 válasz

szavazat
58

Az egyik probléma a kódot Paul Shapiro, hogy nem foglalkozik, ha hozzá kommentárokat alul, ahol a felhasználó keres abban a pillanatban. Azok kommentárok lebegnek a levegőben, mielőtt célba, mert beköltözött a felhasználó látható térkép len.

A másik az, hogy ez is csökken a felhasználó tartózkodási helyét kék pont. Ezzel alábbi kódot, akkor kezelni mind a felhasználó tartózkodási helyét, és nagy mennyiségű térkép kommentárok off-screen. Én is hozzá egy szép ugrál;)

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    MKAnnotationView *aV; 

    for (aV in views) {

        // Don't pin drop if annotation is user location
        if ([aV.annotation isKindOfClass:[MKUserLocation class]]) {
            continue;
        }

        // Check if current annotation is inside visible map rect, else go to next one
        MKMapPoint point =  MKMapPointForCoordinate(aV.annotation.coordinate);
        if (!MKMapRectContainsPoint(self.mapView.visibleMapRect, point)) {
            continue;
        }

        CGRect endFrame = aV.frame;

        // Move annotation out of view
        aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - self.view.frame.size.height, aV.frame.size.width, aV.frame.size.height);

        // Animate drop
        [UIView animateWithDuration:0.5 delay:0.04*[views indexOfObject:aV] options:UIViewAnimationCurveLinear animations:^{

            aV.frame = endFrame;

        // Animate squash
        }completion:^(BOOL finished){
            if (finished) {
                [UIView animateWithDuration:0.05 animations:^{
                    aV.transform = CGAffineTransformMakeScale(1.0, 0.8);

                }completion:^(BOOL finished){
                    [UIView animateWithDuration:0.1 animations:^{
                        aV.transform = CGAffineTransformIdentity;
                    }];
                }];
            }
        }];
    }
}
Válaszolt 12/08/2011 21:24
a forrás felhasználó

szavazat
52

Először is meg kell tenni az nézetvezérlőben végre MKMapViewDelegate, ha nem tette meg.

Aztán végre ezt a módszert:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
   MKAnnotationView *aV; 
   for (aV in views) {
     CGRect endFrame = aV.frame;

     aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - 230.0, aV.frame.size.width, aV.frame.size.height);

     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:0.45];
     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
         [aV setFrame:endFrame];
     [UIView commitAnimations];

   }
}

Add el a kommentárokat a MapView és mikor adunk, ez a módszer küldötte fogják hívni, és animálni a csapokat fentről lefelé, ahogy adunk.

Az értékek az időzítés és a pozicionálási lehet változtatni egy kicsit, de én már csípett, hogy úgy tűnjön a legjobb / legközelebb a hagyományos csepp (amennyire én már tesztelt). Remélem ez segít!

Válaszolt 18/01/2010 16:52
a forrás felhasználó

szavazat
12

Vagy ha még van egy MKAnnotationView alosztály, akkor didMoveToSuperviewkiváltani az animációt. A következő nem egy csepp amelyeknek a végén egy kis „giccs” hatás

  #define kDropCompressAmount 0.1

  @implementation MyAnnotationViewSubclass

  ...

  - (void)didMoveToSuperview {
      CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
      animation.duration = 0.4;
      animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
      animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, -400, 0)];
      animation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];

      CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform"];
      animation2.duration = 0.10;
      animation2.beginTime = animation.duration;
      animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
      animation2.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DMakeTranslation(0, self.layer.frame.size.height*kDropCompressAmount, 0), 1.0, 1.0-kDropCompressAmount, 1.0)];
      animation2.fillMode = kCAFillModeForwards;

      CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"transform"];
      animation3.duration = 0.15;
      animation3.beginTime = animation.duration+animation2.duration;
      animation3.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
      animation3.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
      animation3.fillMode = kCAFillModeForwards;

      CAAnimationGroup *group = [CAAnimationGroup animation];
      group.animations = [NSArray arrayWithObjects:animation, animation2, animation3, nil];
      group.duration = animation.duration+animation2.duration+animation3.duration;
      group.fillMode = kCAFillModeForwards;

      [self.layer addAnimation:group forKey:nil];
  }
Válaszolt 26/06/2010 19:56
a forrás felhasználó

szavazat
5

Michael Tyson válasz (nem tudok mindenhol még), azt javaslom, egy beillesztését az alábbi kódot didMoveToSuperview megfelelő újrafelhasználását MKAnnotationView így működik az animáció újra, majd utánozni a szekvenciális hozzáadásával kommentárok

Játssz a térelválasztó és szorzók a különböző vizuális eredmények ...

- (void)didMoveToSuperview {
    //necessary so it doesn't add another animation when moved to superview = nil
    //and to remove the previous animations if they were not finished!
    if (!self.superview) {
        [self.layer removeAllAnimations];
        return;
    }


    float xOriginDivider = 20.;
    float pos = 0;

    UIView *mySuperview = self.superview;
    while (mySuperview && ![mySuperview isKindOfClass:[MKMapView class]])
        mySuperview = mySuperview.superview;
    if ([mySuperview isKindOfClass:[MKMapView class]]) 
        //given the position in the array
        // pos = [((MKMapView *) mySuperview).annotations indexOfObject:self.annotation];   
        // left to right sequence;
        pos = [((MKMapView *) mySuperview) convertCoordinate:self.annotation.coordinate toPointToView:mySuperview].x / xOriginDivider;

    float yOffsetMultiplier = 20.;
    float timeOffsetMultiplier = 0.05;


    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.4 + timeOffsetMultiplier * pos;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, -400 - yOffsetMultiplier * pos, 0)];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];

    // rest of animation group...
}
Válaszolt 02/11/2010 11:08
a forrás felhasználó

szavazat
0

Azt hiszem, a jobb lehetőség, hogy állítsa az „animatesDrop” YES. Ez egy tulajdonsága MKPinAnnotationView.

Válaszolt 10/11/2014 00:09
a forrás felhasználó

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more