2011-12-17 32 views
9

Tôi tự hỏi nếu có bất cứ điều gì để animate một UIImage.Có thể tạo hoạt ảnh cho một UIImage không?

Tôi biết rằng UIImageViews có thể hoạt ảnh nhưng có cách nào để thực hiện trực tiếp trong UIImage.

Có thể với Open GL ES hoặc một cái gì đó?

Hoặc có bất kỳ cách nào khác để bạn có thể tạo ảnh động MPMediaItemArtwork không?

Cảm ơn trước!

+0

Bạn đang tìm kiếm loại hoạt hình nào? Dịch thuật và chuyển động đơn giản? hoặc thay đổi màn hình? – richerd

+0

Thay đổi giữa nhiều khung hình cơ bản, giống như một .gif. Giống như lần đầu tiên hiển thị image1 sau đó image2 sau đó image3 vv. –

Trả lời

12

Tạo một UIImageView và thiết lập thuộc tính của animationImages tới một mảng của UIImage s

Dưới đây là một ví dụ:

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

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

Nếu bạn đang nhắm mục tiêu iOS 5 bạn có thể làm điều này trực tiếp trong UIImage không số UIImageView sử dụng

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

ví dụ:

[UIImage animatedImagesWithImages:animationFrames duration:10]; 
8

Nếu bạn có nghĩa là hình ảnh động với một vài hình ảnh, sử dụng mã này:

// 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

Bạn có thể sử dụng này.

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

cũng thêm khung lõi thạch anh. – alok

1

Kiểm tra UIImage's +animatedImageWithImages:duration:+animatedImageNamed:duration:.

Từ UIImage Class Reference:

Tạo và trả về một hình ảnh hoạt hình từ một bộ hiện có của hình ảnh.

Phương pháp này tải một loạt tệp bằng cách nối thêm một chuỗi số vào tên tệp cơ sở được cung cấp trong thông số tên. Ví dụ: nếu thông số tên có 'hình ảnh' là nội dung của nó, phương pháp này sẽ cố gắng tải hình ảnh từ các tệp có tên ‘image0’, ‘image1’ và cứ như vậy lên đến ‘image1024’. Tất cả hình ảnh được bao gồm trong hình ảnh động nên chia sẻ cùng kích thước và tỷ lệ.

Các vấn đề liên quan