2011-12-17 95 views
9

我想知道是否有反應動畫UIImage是否可以爲UIImage設置動畫?

我知道UIImageViews可以動畫,但有什麼辦法直接在UIImage

也許與Open GL ES或其他?

或者還有其他方法可以動畫MPMediaItemArtwork嗎?

在此先感謝!

+0

你在找什麼樣的動畫?簡單的翻譯和運動?或更改顯示? – richerd 2011-12-17 13:45:40

+0

基本上在多個幀之間切換,有點像.gif。像首先顯示image1然後image2然後image3等 – 2011-12-17 13:47:45

回答

12

創建UIImageViewanimationImages的屬性設置爲UIImage小號

這裏的數組是一個例子:

NSArray *animationFrames = [NSArray arrayWithObjects: 
    [UIImage imageWithName:@"image1.png"], 
    [UIImage imageWithName:@"image2.png"], 
    nil]; 

UIImageView *animatedImageView = [[UIImageView alloc] init]; 
animatedImageView.animationImages = animationsFrame; 
[animatedImageView startAnimating]; 

如果您定位的iOS 5,你可以在UIImage直接做這沒有UIImageView使用

+(UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration 

例如,

[UIImage animatedImagesWithImages:animationFrames duration:10]; 
8

如果你的意思是用幾張圖片動畫,使用此代碼:

// create the view that will execute our animation 
UIImageView* YourImageView = [[UIImageView alloc] initWithFrame:self.view.frame]; 

// load all the frames of our animation 
YourImageView.animationImages = [NSArray arrayWithObjects:  
          [UIImage imageNamed:@"01.gif"], 
          [UIImage imageNamed:@"02.gif"], 
          [UIImage imageNamed:@"03.gif"], 
          [UIImage imageNamed:@"04.gif"], 
          [UIImage imageNamed:@"05.gif"], 
          [UIImage imageNamed:@"06.gif"], 
          [UIImage imageNamed:@"07.gif"], 
          [UIImage imageNamed:@"08.gif"], 
          [UIImage imageNamed:@"09.gif"], 
          [UIImage imageNamed:@"10.gif"], 
          [UIImage imageNamed:@"11.gif"], 
          [UIImage imageNamed:@"12.gif"], 
          [UIImage imageNamed:@"13.gif"], 
          [UIImage imageNamed:@"14.gif"], 
          [UIImage imageNamed:@"15.gif"], 
          [UIImage imageNamed:@"16.gif"], 
          [UIImage imageNamed:@"17.gif"], nil]; 

// all frames will execute in 1.75 seconds 
YourImageView.animationDuration = 1.75; 
// repeat the animation forever 
YourImageView.animationRepeatCount = 0; 
// start animating 
[YourImageView startAnimating]; 
// add the animation view to the main window 
[self.view addSubview:YourImageView]; 

Source

0

您可以使用此。

arrayOfImages=[[NSMutableArray alloc]init]; 
for(int i=1;i<=54;i++) 
{ 
NSString *[email protected]"haKsequence.png"; 
NSString *changedString= [NSString stringWithFormat:@"%d", i]; 
NSString *stringWithoutSpaces = [nameResources  stringByReplacingOccurrencesOfString:@"K" withString:changedString]; 
[arrayOfImages addObject:(id)[UIImage imageNamed:stringWithoutSpaces].CGImage]; 
} 


self.view.userInteractionEnabled=NO; 
i1.hidden=YES; 
image1=[[UIImageView alloc]init]; 
image1.backgroundColor=[UIColor clearColor]; 
image1.frame = button.frame;//i1 is obshaped buttion 
[self.view addSubview:image1]; 
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"]; 
animation.calculationMode = kCAAnimationDiscrete; 
animation.duration = 1; 
animation.values = arrayOfMonkeyImages; 
[animation setDelegate:self]; 
animation.repeatCount=3; 
[animation setRemovedOnCompletion:YES]; 
[image1.layer addAnimation:animation forKey:@"animation"]; 
+0

以及添加石英核心框架。 – alok 2013-05-08 11:56:02

1

檢查的UIImage的+animatedImageWithImages:duration:+animatedImageNamed:duration:

UIImage Class Reference來自:

創建並從現有組圖像返回的動畫圖像。

此方法通過在name參數中提供的基本文件名後附加一系列數字來加載一系列文件。例如,如果名稱參數具有「圖像」作爲其內容,則此方法會嘗試從名稱爲「image0」,「image1」等的文件加載圖像直至「image1024」。包含在動畫圖像中的所有圖像應該具有相同的大小和比例。

相關問題