2013-10-15 30 views
2

我已經看過動畫了,我無法弄清楚如何在這個video(youtube)中做類似的事情。我想討論它是如何製造的。我不認爲他們在使用精靈。我有一個想法如何做到這一點:例如,我想創建「走路」動物動畫(當動物移動時,他的腿「跑步」移動動畫),我應該創建自定義動物身體視圖3和兩個imageViews動物腿2。然後,我製作了移動腿的硬編碼動畫,瞧,我有自定義動畫。當我移動customView時,我應該開始動畫腿。但有沒有更好的方法來做到這一點?謝謝!定製動畫的對象(動物)

enter image description here enter image description here

+0

你或許應該考慮雪碧處理現有的庫。我使用cocos2d。然而,你的問題是基於意見的,而我即將關閉它。[programmer.se] –

+0

可能更好,這只是我想知道如何使用UIKit – ignotusverum

+0

SpriteKit可能更多使用 –

回答

2

對於一個簡單的效果,你可以保存左腳和右腳作爲單獨的圖像,然後你可以調用重複,autoreversing動畫獨立地,每個旋轉腿部圖像的屬性,例如,是這樣的:

// start one foot animation immediately 

[UIView animateWithDuration:0.25 animations:^{ 
    self.leftFootImageView.transform = [self calculateTransformForFootToAngle:M_PI/8.0]; 
} completion:^(BOOL finished) { 
    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ 
     self.leftFootImageView.transform = [self calculateTransformForFootToAngle:-M_PI/8.0]; 
    } completion:NULL]; 
}]; 

// delay the second foot's animation a bit, if you want 
// (if you don't want to change the timing of the right foot, you 
// could just combine the setting of the right foot's `transform` 
// in the above block) 

double delayInSeconds = 0.1; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    [UIView animateWithDuration:0.25 animations:^{ 
     self.rightFootImageView.transform = [self calculateTransformForFootToAngle:M_PI/8.0]; 
    } completion:^(BOOL finished) { 
     [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ 
      self.rightFootImageView.transform = [self calculateTransformForFootToAngle:-M_PI/8.0]; 
     } completion:NULL]; 
    }]; 
}); 

顯然,你需要計算CGAffineTransform值的方法:

- (CGAffineTransform)calculateTransformForFootToAngle:(CGFloat)angle 
{ 
    // translate up, rotate, and then translate back, so that the rotation takes place at the top of the leg 

    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -15); 
    transform = CGAffineTransformRotate(transform, angle); 
    return CGAffineTransformTranslate(transform, 0, 15); 
} 

上述假定您已經定義了個人的腳的圖像是非常小的,只是邊界腳,而不是所有的空白。您必須充分利用適合您圖片的翻譯值。

最後,當你想停止動畫,然後你可以只是動畫雙腿回到恆等變換:

[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ 
    self.leftFootImageView.transform = CGAffineTransformIdentity; 
    self.rightFootImageView.transform = CGAffineTransformIdentity; 
} completion:NULL]; 
+0

哇,謝謝。但是我想知道,例如,我會有10-15個這樣的對象,它會變得遲鈍嗎? – ignotusverum

+0

@anonymous只要你正在執行這個'transform'就是小圖像視圖,它應該非常高效。我有64條腿在iPhone 3GS上以不同的速度擺動,並且在60fps時非常平滑。當然,在某些情況下,你最終會遇到退化,但它效率驚人(如果你小心)。 – Rob

+1

@anonymous順便說一句,看這段視頻,他們可能也會使用'CAKeyFrame'動畫(例如,向一個方向擺動一點,然後向另一個方向擺動一下,然後重複)。 – Rob

2

你必須要使用的每一個照片。因此,在Photoshop(或您的軟件)中,您可以讓動畫「跑步」的動畫顯示30張圖片,所有分辨率和尺寸都相同(在每張照片中移動腿部)您只需要一個圖像視圖,而不是3個。請記住,您的常規視頻是每秒30幀。所以如果你想讓這個動畫播放一秒鐘,你會想要至少有20張圖片。繼承人,你要去的代碼想在.m文件使用方法:

- (IBAction)animation{ 
animationView.animationImages = [NSArray arrayWithObjects: 
            [UIImage imageNamed:@"PR-P-1"], //use your image names here, put the names in in order, I recommend making the images initially with a number to tell you which one in the sequence it is 
            [UIImage imageNamed:@"PR-P-2"], 
            [UIImage imageNamed:@"PR-P-3"], 
            [UIImage imageNamed:@"PR-P-4"], 
            [UIImage imageNamed:@"PR-P-5"], 
            [UIImage imageNamed:@"PR-P-6"], 
            [UIImage imageNamed:@"PR-P-7"], 
            [UIImage imageNamed:@"PR-P-8"], 
            [UIImage imageNamed:@"PR-P-9"], 
            [UIImage imageNamed:@"PR-P-10"], 
            [UIImage imageNamed:@"PR-P-11"], 
            [UIImage imageNamed:@"PR-P-12"], 
            [UIImage imageNamed:@"PR-P-13"], 
            [UIImage imageNamed:@"PR-P-14"], 
            [UIImage imageNamed:@"PR-P-15"], 
            [UIImage imageNamed:@"PR-P-16"], 
            [UIImage imageNamed:@"PR-P-17"], 
            [UIImage imageNamed:@"PR-P-18"], 
            [UIImage imageNamed:@"PR-P-19"], 
            [UIImage imageNamed:@"PR-P-20"], 
            [UIImage imageNamed:@"PR-P-21"], 
            [UIImage imageNamed:@"PR-P-22"], 
            [UIImage imageNamed:@"PR-P-23"], 
            [UIImage imageNamed:@"PR-P-24"], 
            [UIImage imageNamed:@"PR-P-25"], 
            [UIImage imageNamed:@"PR-P-26"], 
            [UIImage imageNamed:@"PR-P-27"], 
            [UIImage imageNamed:@"PR-P-28"],nil]; //make sure to have nil! 


animationView.animationRepeatCount = 1; //sets how many times it repeats, do a huge number for infinate 
animationView.animationDuration = 2.5; //sets how long one repeat takes 
[animationView startAnimating]; //starts the animation 

} 

然後在您的.h文件中聲明IBOutlet這應該是一個UIImageView名爲animationView

如果想要另一個動畫,只是使用與上面相同的代碼,更改IBAction的名稱並在您的.h文件中創建另一個IBOutlet,該文件將再次爲UIImageView,並將其命名爲IBAction,例如,如果您製作了另一個IBAction稱爲playViolin,你將在你的.h文件中使用IBOutlet playViolin

然後在您的.xib文件中,創建一個新的圖像視圖,並從文件的所有者掛接animationView並將其設置。當你想讓它開始時,請致電[self animation]

1

這是一個很好的教程它在哪裏好explaine:

http://www.appcoda.com/ios-programming-animation-uiimageview/

+1

雖然此鏈接可能回答問題,但最好在此處包含答案的基本部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/18997573) – dpassage