我有一个实例MKMapView
并且想使用自定义注释图标而不是 MKPinAnnotationView 提供的标准图钉图标。因此,我设置了一个名为 CustomMapAnnotation 的 MKAnnotationView 子类,并重写-(void)drawRect:
以绘制 CGImage。这行得通。
当我尝试复制.animatesDrop
MKPinAnnotationView 提供的功能时,问题就来了;MKMapView
当注释被添加到实例时,我希望我的图标逐渐出现,从上往下并以从左到右的顺序出现。
这是 -(void)drawRect: 用于 CustomMapAnnotation,它在您仅绘制 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 建议,那也很棒。