是否可以创建自定义的动画 MKAnnotationView?
我正在尝试在 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 不断重绘,使其看起来是动画的?
I'm trying to simulate the user location animation in MapKit (where-by the user's position is represented by a pulsating blue dot). I've created a custom subclass of MKAnnotationView and in the drawRect method I'm attempting to cycle through a set of colors. Here's a simpler implementation of what I'm doing:
- (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;
}
Unfortunately this just causes the annotations to be one of the 3 different colors and doesn't cycle through them. Is there a way to force the MKAnnotations to continually redraw so that it appears to be animated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当您希望重绘注释视图时,您都可以自由地在注释视图上调用
setNeedsDisplay
。最简单的方法是让注释视图本身设置一个计时器,每隔 1/60 秒左右触发一次。更复杂的方法是将绘图代码放入自定义的 CALayer 中,并向其应用重复的核心动画动画。请参阅我对“对 CALayer 子类的自定义属性进行动画处理”的回答< /a> 的一种方法。
You are free to call
setNeedsDisplay
on your annotation view whenever you want it to redraw. The easiest way to do this would be for the annotation view itself to set up a timer that fired every 1/60th of a second or so.A more sophisticated approach would be to put your drawing code into a custom
CALayer
and apply a repeating Core Animation animation to it. See my answer to "Animating a custom property of CALayer subclass" for an approach.