代碼如下:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *tom;
@end
@implementation ViewController
- (void)tomAnimationOfName:(NSString *) img andCount:(int)count
{
? ?//序列幀動畫:讓一組圖片,和放電影一樣,一張張的播放,動起來的效果
? ?//判斷是否動畫中,不可以同步
? ?if ([self.tom isAnimating]) {
? ? ? ?//直接結束動畫操作方法,這里就是沒有返回值,nil 不行
? ? ? ?return;
? ?}
? ?//開始肯定是存儲圖片,81個,用可變數組
? ?NSMutableArray *arrayImage = [NSMutableArray array];
? ?//遍歷圖片
? ?for (int i = 0; i < count; i++) {
? ? ? ?//類似 c,格式控制,不足兩位0補齊
? ? ? ?NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg", img, i];
? ? ? ?UIImage *image = [UIImage imageNamed:name];
? ? ? ?//添加到數組里
? ? ? ?[arrayImage addObject:image];
? ?}
? ?//然后開始動畫
? ?//把圖片放到animationImages,接受數組參數
? ?self.tom.animationImages = arrayImage;
? ?//設置時間間隔,81張圖,圖多就播的時間稍微長,否則短
? ?self.tom.animationDuration = arrayImage.count * 0.074;
? ?//設置重復次數
? ?self.tom.animationRepeatCount = 1;
? ?//開始動畫
? ?[self.tom startAnimating];
? ?//結束動畫
}
- (IBAction)head
{
? ?[self tomAnimationOfName:@"knockout" andCount:81];
}
- (IBAction)drink
{
? ?[self tomAnimationOfName:@"drink" andCount:81];
}