iOS:在移动图钉上显示标注视图
iOS : Show the Callout view on moving pin
我实现了与 Uber iOS
应用程序相同的功能,用于根据带有动画的路线移动图钉。
问题: 当我在图钉移动时单击它时,我无法获得 MKMapView
的 didSelectAnnotationView
委托。但是当 pin 稳定意味着不动时,它就会被调用。
代码: 创建 pin
-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
else {
NSString *annotationIdentifier = @"CustomViewAnnotation";
AnnotationView * annotationView = (AnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if(annotationView == nil) {
annotationView = [[AnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:annotationIdentifier];
UIImage *pinIcon = [UIImage imageNamed:@"carIcon"];
annotationView.btnInfo = [[UIButton alloc] init];
annotationView.frame = CGRectMake(0.0f, 0.0f, pinIcon.size.width, pinIcon.size.height);
annotationView.btnInfo.frame = annotationView.frame;
[annotationView.btnInfo setBackgroundImage:pinIcon forState:UIControlStateNormal];
[annotationView addSubview:annotationView.btnInfo];
[annotationView.btnInfo setUserInteractionEnabled:NO];
}
else
annotationView.annotation = annotation;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
return annotationView;
}
}
更新坐标
- (void)updateLocation {
CLLocationCoordinate2D oldLocation;
CLLocationCoordinate2D newLocation;
oldLocation.latitude = [self convertStringToFloat:self.arrayCSV[self.index-1][@"Latitude"]];
oldLocation.longitude = [self convertStringToFloat:self.arrayCSV[self.index-1][@"Longitude"]];
newLocation.latitude = [self convertStringToFloat:self.arrayCSV[self.index][@"Latitude"]];
newLocation.longitude = [self convertStringToFloat:self.arrayCSV[self.index][@"Longitude"]];
float getAngle = [self angleFromCoordinate:oldLocation toCoordinate:newLocation];
CLLocation *oldInfo = [[CLLocation alloc] initWithLatitude:oldLocation.latitude longitude:oldLocation.longitude];
CLLocation *newInfo = [[CLLocation alloc] initWithLatitude:newLocation.latitude longitude:newLocation.longitude];
[UIView animateWithDuration:3
animations:^{
myAnnotation.coordinate = newLocation;
AnnotationView *annotationView = (AnnotationView *)[self.mapView viewForAnnotation:myAnnotation];
annotationView.transform = CGAffineTransformMakeRotation(getAngle);
}];
}
试试这个。
我认为您看到的行为是因为,默认情况下,用户交互在新块调用时禁用持续动画。
您可以通过传递 UIViewAnimationOptionAllowUserInteraction 来覆盖它,如下所示:
[UIView animateWithDuration:3
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
myAnnotation.coordinate = newLocation;
AnnotationView *annotationView = (AnnotationView *)[self.mapView viewForAnnotation:myAnnotation];
annotationView.transform = CGAffineTransformMakeRotation(getAngle);
}
completion:nil];
我实现了与 Uber iOS
应用程序相同的功能,用于根据带有动画的路线移动图钉。
问题: 当我在图钉移动时单击它时,我无法获得 MKMapView
的 didSelectAnnotationView
委托。但是当 pin 稳定意味着不动时,它就会被调用。
代码: 创建 pin
-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
else {
NSString *annotationIdentifier = @"CustomViewAnnotation";
AnnotationView * annotationView = (AnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if(annotationView == nil) {
annotationView = [[AnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:annotationIdentifier];
UIImage *pinIcon = [UIImage imageNamed:@"carIcon"];
annotationView.btnInfo = [[UIButton alloc] init];
annotationView.frame = CGRectMake(0.0f, 0.0f, pinIcon.size.width, pinIcon.size.height);
annotationView.btnInfo.frame = annotationView.frame;
[annotationView.btnInfo setBackgroundImage:pinIcon forState:UIControlStateNormal];
[annotationView addSubview:annotationView.btnInfo];
[annotationView.btnInfo setUserInteractionEnabled:NO];
}
else
annotationView.annotation = annotation;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
return annotationView;
}
}
更新坐标
- (void)updateLocation {
CLLocationCoordinate2D oldLocation;
CLLocationCoordinate2D newLocation;
oldLocation.latitude = [self convertStringToFloat:self.arrayCSV[self.index-1][@"Latitude"]];
oldLocation.longitude = [self convertStringToFloat:self.arrayCSV[self.index-1][@"Longitude"]];
newLocation.latitude = [self convertStringToFloat:self.arrayCSV[self.index][@"Latitude"]];
newLocation.longitude = [self convertStringToFloat:self.arrayCSV[self.index][@"Longitude"]];
float getAngle = [self angleFromCoordinate:oldLocation toCoordinate:newLocation];
CLLocation *oldInfo = [[CLLocation alloc] initWithLatitude:oldLocation.latitude longitude:oldLocation.longitude];
CLLocation *newInfo = [[CLLocation alloc] initWithLatitude:newLocation.latitude longitude:newLocation.longitude];
[UIView animateWithDuration:3
animations:^{
myAnnotation.coordinate = newLocation;
AnnotationView *annotationView = (AnnotationView *)[self.mapView viewForAnnotation:myAnnotation];
annotationView.transform = CGAffineTransformMakeRotation(getAngle);
}];
}
试试这个。
我认为您看到的行为是因为,默认情况下,用户交互在新块调用时禁用持续动画。
您可以通过传递 UIViewAnimationOptionAllowUserInteraction 来覆盖它,如下所示:
[UIView animateWithDuration:3
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
myAnnotation.coordinate = newLocation;
AnnotationView *annotationView = (AnnotationView *)[self.mapView viewForAnnotation:myAnnotation];
annotationView.transform = CGAffineTransformMakeRotation(getAngle);
}
completion:nil];