2011-11-25 56 views
0

我想創建一個圖像動畫,使圖像來回擺動,非常像節拍器。動畫的目標是模擬「微調」效果。想象一下,只有上半部分,箭頭會前後移動,直到它停在目標上。 enter image description hereUIImageView的節拍器式動畫iOS

到目前爲止,我已經能夠讓箭頭來回移動,但動畫的錨點是圖像的中心。我希望這個錨點成爲圖像的底部中心。爲了模擬節拍器,錨點不能在當前位置。我如何去做這個改變?動畫代碼如下:

#define RADIANS(degrees) ((degrees * M_PI)/180.0) 


CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(90)); 
CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-90)); 

arrow.transform = leftWobble; // starting point 

[UIView beginAnimations:@"wobble" context:(__bridge void *)arrow]; 
[UIView setAnimationRepeatAutoreverses:YES]; // important 
[UIView setAnimationRepeatCount:10]; 
[UIView setAnimationDuration:.5]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(wobbleEnded:finished:context:)]; 

arrow.transform = rightWobble; // end here & auto-reverse 

[UIView commitAnimations]; 

回答

0

CALayer有一個anchorPoint屬性,您可以將其設置爲箭頭圖像的底部。

蘋果有一個名爲節拍器的iOS示例項目。我似乎無法再在網上找到它,所以它可能已被刪除,但這裏有一個代碼片段:

// Set up the metronome arm. 
    metronomeArm = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"metronomeArm.png"]]; 

    // Move the anchor point to the bottom middle of the metronomeArm bounds, so rotations occur around that point. 
    metronomeArm.layer.anchorPoint = CGPointMake(0.5, 1.0); 
+0

什麼單位是'anchorPoint' in?顯然他們是X和Y點值。因此,對於圖像的底部中心,我會沿着'CGPointMake(arrow.bounds.size.width/2,arrow.bounds.size.height)'的方向做些什麼? –

+0

Apple樣本的片段顯示它與圖像的尺寸有關:0.5,1.0表示水平居中,垂直居中。因此,您可以完全按照原樣進行操作,因爲您需要相同的定位點。 – Clafou

+0

謝謝,不知道anchorPoint屬性存在。 –