您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS:將多張圖片整合成連續動畫

編輯:iOS 相關資訊

把多張圖片整合成連續動畫,是很多 iOS 開發者會用到的功能。透過 UIImageView 的 animationImages 屬性,就能輕鬆實現。以下分別示範單次(有限次)播放與循環播放的程式碼。

有限次播放

animationImageView.animationImages = imageFrames; // imageFrames 是一個圖片陣列
[UIView setAnimationDelegate:self];
animationImageView.animationDuration = 0.75f;
animationImageView.animationRepeatCount = 3;
[animationImageView startAnimating];
shouldPlay = NO;
NSInteger timeout = 3;
[NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(stopAnimation) userInfo:nil repeats:NO];

這段程式碼設定動畫的播放時長與重複次數,並透過 NSTimer 在指定時間後呼叫 stopAnimation 停止動畫。

循環播放

若將 animationRepeatCount 設為 0,動畫便會無限循環播放:

UIImageView *animView = [[UIImageView alloc] initWithFrame:self.view.frame];
animView.animationImages = [NSArray arrayWithObjects:
    [UIImage imageNamed:@"01.gif"],
    [UIImage imageNamed:@"02.gif"],
    [UIImage imageNamed:@"03.gif"], nil];

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

透過 animationImages 提供圖片陣列、設定 animationDuration 控制播放速度、再用 animationRepeatCount 決定播放次數,就能靈活實現單次或循環的連續動畫效果。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved