【问题标题】:Is it possible to create a custom, animated MKAnnotationView?是否可以创建自定义的动画 MKAnnotationView?
【发布时间】:2010-04-21 13:47:40
【问题描述】:

我正在尝试在 MapKit 中模拟用户位置动画(其中用户的位置由一个脉动的蓝点表示)。我已经创建了 MKAnnotationView 的自定义子类,并且在 drawRect 方法中我试图在一组颜色之间循环。这是我正在做的更简单的实现:

- (void)drawRect:(CGRect)rect {
float magSquared = event.magnitude * event.magnitude;
CGContextRef context = UIGraphicsGetCurrentContext();
if (idx == -1) {
    r[0] = 1.0; r[1] = 0.5; r[2] = 0;
    b[0] = 0; b[1] = 1.0; b[2] = 0.5;
    g[0] = 0.5; g[1] = 0; g[2] = 1.0;
    idx = 0;
}
// CGContextSetRGBFillColor(context, 1.0, 1.0 - magSquared * 0.015, 0.211, .6);
CGContextSetRGBFillColor(context, r[idx], g[idx], b[idx], 0.75);
CGContextFillEllipseInRect(context, rect);
idx++;
if (idx > 3) idx = 0;
}

不幸的是,这只会导致注释成为 3 种不同颜色之一,并且不会循环显示它们。有没有办法强制 MKAnnotations 不断重绘以使其看起来是动画的?

【问题讨论】:

    标签: iphone mapkit mkannotation


    【解决方案1】:

    您可以随时在注释视图上调用setNeedsDisplay 来重绘它。最简单的方法是注释视图本身设置一个计时器,该计时器每 1/60 秒左右触发一次。

    更复杂的方法是将您的绘图代码放入自定义CALayer 并对其应用重复的核心动画动画。方法见my answer to "Animating a custom property of CALayer subclass"

    【讨论】:

    • 谢谢。添加
      [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
      虽然我肯定会考虑你的 CALayer 建议。
    猜你喜欢
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多