UIImageView 天生就支援幀動畫,只要餵它一個 UIImage 陣列、設定播放次數與每幀間隔,再 startAnimating 就完成了。這比自己在 timer 裡切圖簡單太多,是播放序列幀的首選方案。



- (void)play:(NSString *)filenamePrefix count:(int)count {
// 1. 載入所有的動畫圖片
NSMutableArray *images = [NSMutableArray array];
for (int i = 1; i <= count; i++) {
NSString *filename = [NSString stringWithFormat:@"%@_%d", filenamePrefix, i];
NSString *file = [[NSBundle mainBundle] pathForResource:filename ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:file];
[images addObject:image];
}
// 2. 設定動畫圖片
self.imageView.animationImages = images;
// 3. 設定播放次數:站樁是無限次(0),其餘播一次
self.imageView.animationRepeatCount =
[filenamePrefix isEqualToString:@"stand"] ? 0 : 1;
// 4. 設定靜態底圖(動畫未啟動時顯示的那一張)
self.imageView.image = [UIImage imageNamed:@"stand_1"];
// 5. 設定動畫總時長
self.imageView.animationDuration = count * 0.04;
// 6. 開始播放
[self.imageView startAnimating];
// 站樁是無限循環,不必安排後續
if ([filenamePrefix isEqualToString:@"stand"]) return;
// 7. 播完後回到站樁姿態
[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
}
#import <AVFoundation/AVFoundation.h> @property (strong, nonatomic) AVPlayer *player; // 1. 取得音檔 URL(URL 就是檔案路徑物件) NSURL *url = [[NSBundle mainBundle] URLForResource:@"dazhao" withExtension:@"mp3"]; // 2. 建立播放器 self.player = [AVPlayer playerWithURL:url]; // 3. 播放 [self.player play];
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) AVPlayer *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 加上紅色邊框
CALayer *layer = [self.imageView layer];
layer.borderColor = [UIColor redColor].CGColor;
layer.borderWidth = 5.0f;
[self stand];
}
#pragma mark - 各個動作
- (IBAction)stand { [self play:@"stand" count:10]; }
- (IBAction)dead { [self play:@"dead" count:23]; }
- (IBAction)run { [self play:@"run" count:6]; }
// 小招:三段動畫串接,每段播完再播下一段
- (IBAction)xiaozhao {
[self play:@"xiaozhao1" count:20];
[self playMusic:@"xiaozhao1"];
[self performSelector:@selector(xiaozhao2) withObject:nil afterDelay:20 * 0.06];
}
- (IBAction)dazhao {
[self play:@"dazhao" count:87];
[self playMusic:@"dazhao"];
}
- (IBAction)install_b { [self play:@"install_b" count:29]; }
#pragma mark - 續播
- (void)xiaozhao2 {
[self play:@"xiaozhao2" count:35];
[self playMusic:@"xiaozhao2"];
[self performSelector:@selector(xiaozhao3) withObject:nil afterDelay:35 * 0.06];
}
- (void)xiaozhao3 {
[self play:@"xiaozhao3" count:39];
[self playMusic:@"xiaozhao3"];
}
#pragma mark - 音樂
- (void)playMusic:(NSString *)music {
NSURL *url = [[NSBundle mainBundle] URLForResource:music withExtension:@"mp3"];
self.player = [AVPlayer playerWithURL:url];
[self.player play];
}
#pragma mark - 播放動畫
- (void)play:(NSString *)preFixName count:(int)count {
NSMutableArray *images = [NSMutableArray array];
for (int i = 1; i <= count; i++) {
NSString *fileName = [NSString stringWithFormat:@"%@_%d", preFixName, i];
NSString *name = [[NSBundle mainBundle] pathForResource:fileName ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:name];
[images addObject:image];
}
self.imageView.animationImages = images;
self.imageView.image = [UIImage imageNamed:@"stand_1"];
self.imageView.animationRepeatCount =
[preFixName isEqualToString:@"stand"] ? 0 : 1;
self.imageView.animationDuration = count * 0.06;
[self.imageView startAnimating];
if ([preFixName isEqualToString:@"stand"]) return;
[self performSelector:@selector(stand) withObject:nil afterDelay:count * 0.06];
}
@end
imageNamed: 通常更好。前綴_1.png 到 前綴_count.png 連續編號。若某張缺號,imageWithContentsOfFile: 會得到 nil 而被加入陣列,播放時可能整段中斷。建議加入 nil 檢查。cancelPreviousPerformRequestsWithTarget:selector:object: 清掉舊的排程。SpriteKit 的 texture atlas,或把序列圖打包成 SpriteSheet/使用 CAKeyframeAnimation 搭配少量圖。count * 0.04 是「總時長」而不是每幀間隔——animationDuration 代表整段動畫播放完所需的秒數,系統會用它除以幀數算出每幀時間。這點容易誤解。