掉落的图钉的动画不起作用

Posted

技术标签:

【中文标题】掉落的图钉的动画不起作用【英文标题】:Animations for a dropped pin not working 【发布时间】:2011-02-16 14:10:21 【问题描述】:

当我在我的 mkmapview 上放置一个图钉时,动画效果很好,并且图钉按预期从屏幕顶部掉落。我把那个别针涂成红色,因为我还没有验证它。但是,如果我使用一些在 calloutAccessoryControlTapped 上调用的代码验证引脚,我会将引脚的颜色更改为绿色。

当我在地图上按下另一个红色大头针时,动画失败,大头针刚刚出现。如果我再做一次(长按),就会出现一个新的图钉,它的动画很好。我可以对导致这种行为的注释做些什么。

感谢您提供有关此处可能出现问题的任何见解... 多尼

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 


    if ([annotation isKindOfClass:[MKUserLocation class]]) 
    
        return nil;     
    

    static NSString * const kPinIdentifier = @"PinIdentifier";
    MKPinAnnotationView *draggablePinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:kPinIdentifier];

    if(!draggablePinView)
    
        draggablePinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kPinIdentifier] autorelease];
        draggablePinView.pinColor = MKPinAnnotationColorRed;
        draggablePinView.animatesDrop = YES;
        draggablePinView.canShowCallout = YES;
        draggablePinView.draggable = YES;


        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        draggablePinView.rightCalloutAccessoryView = rightButton;
    
    else
    
        draggablePinView.annotation = annotation;
        draggablePinView.animatesDrop = YES;
    
    return draggablePinView;



-(void)longPressGesture:(UIGestureRecognizer *)gestureRecognizer
    
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    
        CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
        CLLocationCoordinate2D touchMapCoordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView];

        [self dropPinWithCoords:touchMapCoordinate];
    


- (void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
        
    [self getFromServer];


-(void) showLoc8CodeDetail:(LOC8Code *) loc8

    // Open the view controller
    viewLoc8ViewController *loc8view = [[viewLoc8ViewController alloc] initWithLoc8code:loc8];
    [gMainViewController.navigationController pushViewController:loc8view animated:YES];
    [loc8view release];


-(void) dropPinWithCoords:(CLLocationCoordinate2D)loc

    // Set the loc8code from index 0 in the global array and populate from response
    LOC8Code *newPin = [gMapAnnotations objectAtIndex:0];
    newPin.starRating = 1;
    newPin.title = @"Unverified Pin";
    newPin.subtitle = @"Click to verify postcode";
    newPin.verified = NO; // this means you do have to requery if opened
    newPin.coordinate = loc;


    [self removeMapAnnotations];        
    [gMapAnnotations replaceObjectAtIndex:1 withObject:newPin];
    [self.mapView addAnnotation:[gMapAnnotations objectAtIndex:1]];

    // Reset flags
    [dropPinButton setTitle:@"Replace Pin" forState:UIControlStateNormal];
    [self moveAndHidePinButtons:NO];

-(void)getFromServer

    [self checkRegisteredFromServer]; // Does API call to see if device already registered

    // Get the loc8 code from the global array (pin always index 1)
    LOC8Code *loc8 = [gMapAnnotations objectAtIndex:1];

    // If we have a verified pin already (from a search) just show the info
    if(loc8.verified == YES)
    
        [self showLoc8CodeDetail:loc8];
    
    else
    
        [self setSpinning:YES];
        // Get coordinates
        NSString *urlWithCoords = [NSString stringWithFormat:@"https://www.server.com/api/rest/lite/loc?t=%@&u=%@&a=iloc8er&la=%f&lo=%f", 
                                    [Util getAPIToken], [UIDevice currentDevice].uniqueIdentifier, 
                                    loc8.coordinate.latitude,
                                    loc8.coordinate.longitude];
        NSLog(@"%@", urlWithCoords);
        ASIHTTPRequest *request = [self populateRequest:request withURL: urlWithCoords];
        [request setDidFinishSelector:@selector(loc8returned:)];
        [[self networkQueue] addOperation:request]; 
    


-(void)loc8returned:(ASIHTTPRequest *)request

    @try 
        
        NSString *response = [request responseString];
        NSLog(@"RSP: %@", response);

        // Store incoming data into an NSDictionary
        SBJsonParser *parser = [[SBJsonParser new]autorelease];
        NSDictionary *results = [parser objectWithString:response error:nil];

        // Check for errors
        NSNumber *r = [results objectForKey:@"r"];
        NSString *e = [results valueForKey:@"e"];

        if(r.boolValue == NO)
        
            if([e isEqualToString:@"lc51"] || [e isEqualToString:@"lc52"])
            
                // Outside range
                [self setSpinning:NO];
                return;
            

            if([e isEqualToString:@"ud04"])
            
                // quota check
                [self setSpinning:NO];
                return;
            
        
        else
        
            LOC8Code *loc8 = [gMapAnnotations objectAtIndex:1];

            // Set the loc8code
            NSString *lc = [results valueForKey:@"lc"];
            loc8.postcode = lc;
            loc8.verified = YES;
            loc8.starRating = 0;
            loc8.title = lc;


            // This pin is now validated
            MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView viewForAnnotation:[gMapAnnotations objectAtIndex:1]];
            pinView.pinColor = MKPinAnnotationColorGreen;

            // Open the view controller
            [self showLoc8CodeDetail:loc8];
        
    
    @catch (NSException *exception) 
    
        [Util say:@"Service Temporarily Unavailable (-200):"];
        NSLog(@"RSP: %@, %@", [exception name], [exception reason]);
    
    [self setSpinning:NO];

loc8code头类定义如下:

@interface LOC8Code : MKPlacemark 
    NSString *postcode;
    NSString *BusinessName;
    int starRating;
    CLLocationCoordinate2D coordinate_;
    NSString *title_;
    NSString *subtitle_;
    BOOL verified;          // Verified locations can't be moved (if pin moved then flag as not verified)


// Re-declare MKAnnotation's readonly property 'coordinate' to readwrite. 

@property (nonatomic, retain) NSString *postcode;
@property (nonatomic, retain) NSString *BusinessName;
@property (nonatomic, assign) int starRating;
@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, assign) BOOL verified;

@end

以及实现

#import "LOC8Code.h"

@implementation LOC8Code

@synthesize coordinate = coordinate_;
@synthesize title = title_;
@synthesize subtitle = subtitle_;
@synthesize verified;
@synthesize starRating;
@synthesize postcode;
@synthesize BusinessName;


- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary 

    if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary])) 
        self.coordinate = coordinate;
    
    return self;


- (void)dealloc 
    [title_ release];
    [subtitle_ release];
    [postcode release];
    [BusinessName release];

    [super dealloc];


@end

【问题讨论】:

显示 dropPinWithCoords 和 getFromServer 方法(如果它们很长,则只显示相关部分)。 代码已更新。很抱歉没有包含足够的内容 您的问题找到解决方案了吗? 【参考方案1】:

这可能是因为您一直使用相同的重用标识符。看看这条线:static NSString * const kPinIdentifier = @"PinIdentifier";

如果所有这些引脚都不同,则该值对于您放置的每个引脚都必须是唯一的。我经常使用逗号分隔的标记的纬度和经度字符串作为我的标识符。

【讨论】:

刚开始测试这个,它似乎是重用标识符。现在工作正常。谢谢

以上是关于掉落的图钉的动画不起作用的主要内容,如果未能解决你的问题,请参考以下文章

UIView 动画不起作用

动画延迟不起作用

当我调用可见性时,Android 动画不起作用

UIView 块动画不起作用

为啥进入动画在新线程中不起作用?

颤振补间基本动画在“FutureBuilder”中不起作用