2010-04-21 94 views
3

我試圖在MapKit中模擬用戶位置動畫(用戶的位置由脈動藍點表示)。我創建了一個MKAnnotationView的自定義子類,並在drawRect方法中嘗試循環使用一組顏色。這裏有一個簡單的實現的我在做什麼:是否可以創建一個自定義的動畫MKAnnotationView?

- (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不斷重繪,使它看起來像動畫?

回答

4

只要你想重繪,你可以在你的註解視圖上自由調用setNeedsDisplay。最簡單的方法是註釋視圖本身設置一個計時器,每1/60秒就會觸發一次。

更復雜的方法是將您的繪圖代碼放入自定義的CALayer中,並將重複的Core Animation動畫應用到它。有關方法,請參閱my answer to "Animating a custom property of CALayer subclass"

+0

謝謝。添加

[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
的作品,雖然我一定會看看你的CALayer建議。 – smountcastle 2010-04-21 14:57:16

相關問題