[英]Metronome-Like Animation of UIImageView 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];
CALayer具有anchorPoint屬性,您可以將其設置為箭頭圖像的底部。
蘋果有一個名為Metronome的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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.