我有一个
问题在于,当我尝试复制MKPinAnnotationView提供的
这是CustomMapAnnotation的
MKMapView
实例,想要使用自定义注释图标代替MKPinAnnotationView提供的标准标记图标。所以,我设置了一个MKAnnotationView的子类CustomMapAnnotation,并重写了-(void)drawRect:
方法来绘制CGImage,这很有效。问题在于,当我尝试复制MKPinAnnotationView提供的
.animatesDrop
功能时,出现了困难;我希望我的图标逐渐出现,从上方向下降,并按从左到右的顺序添加到MKMapView
实例中。这是CustomMapAnnotation的
-(void)drawRect:
方法,如果只绘制UIImage,则可以正常工作(第二行是绘制UIImage)。- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[((Incident *)self.annotation).smallIcon drawInRect:rect];
if (newAnnotation) {
[self animateDrop];
newAnnotation = NO;
}
}
当你添加animateDrop
方法时问题就出现了:
-(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:@""];
}
它只是不起作用,可能有很多原因。我现在不会深入讨论所有原因。我的主要问题是这个方法是否可行,或者是否应该尝试完全不同的方法。
我还尝试将整个过程打包成动画事务,以便beginTime参数实际起作用;但似乎完全没有任何效果。我不知道这是因为我缺少关键点还是因为MapKit在某种程度上破坏了我的动画。
// Does nothing
[CATransaction begin];
[map addAnnotations:list];
[CATransaction commit];
如果有人对像这样的动画MKMapAnnotations有任何经验,我很想得到一些提示;否则,如果您能就此方法提供CAAnimation建议,那也很好。