gpt4 book ai didi

iphone - 如何使用 MKAnnotationView 创建自定义 "pin-drop"动画?

转载 作者:行者123 更新时间:2023-12-03 18:17:22 25 4
gpt4 key购买 nike

我有一个 MKMapView 实例,并且希望使用自定义注释图标而不是 MKPinAnnotationView 提供的标准图钉图标。因此,我设置了一个名为 CustomMapAnnotation 的 MKAnnotationView 子类,并重写 -(void)drawRect: 来绘制 CGImage。这有效。

当我尝试复制 MKPinAnnotationView 提供的 .animatesDrop 功能时,麻烦就来了;当注释添加到 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 有关该方法的建议,那就太好了。

最佳答案

Paul Shapiro 的代码的一个问题是,当您在用户当前正在查看的位置下方添加注释时,它不会处理。这些注释在掉落之前会漂浮在半空中,因为它们已移动到用户的可见 map 矩形中。

另一个是它还会删除用户位置蓝点。使用下面的代码,您可以在屏幕外处理用户位置和大量 map 注释。我还添加了一个漂亮的弹跳;)

- (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;
}];
}];
}
}];
}
}

关于iphone - 如何使用 MKAnnotationView 创建自定义 "pin-drop"动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1857160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com