1 self.tom.animationImages = images; // 存儲了多張組成動畫的圖片23 [self.tom setAnimationRepeatCount:1]; // 默認0是無限次4 [self.tom setAnimationDuration: images.count/FramesCount];5 [self.tom startAnimating];
1 // imageNamed: 有緩存2 // UIImage *image = [UIImage imageNamed:fileName];34 // imageWithContentOfFile: 沒有緩存(傳入文件的全路徑)5 NSBundle *bundle = [NSBundle mainBundle];6 NSString *path = [bundle pathForResource:fileName ofType:nil];7 UIImage *image = [UIImage imageWithContentsOfFile:path];
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration + 1];1 #import "ViewController.h" 2 3 #define FramesCount 24 // 動畫幀數/秒 4 5 @interface ViewController () 6 @PRoperty (weak, nonatomic) IBOutlet UIImageView *tom; 7 8 - (IBAction)drink; 9 - (IBAction)knockHead;1011 @end1213 @implementation ViewController1415 - (void)viewDidLoad {16 [super viewDidLoad];17 // Do any additional setup after loading the view, typically from a nib.1819 }2021 - (void)didReceiveMemoryWarning {22 [super didReceiveMemoryWarning];23 // Dispose of any resources that can be recreated.24 }2526 /** 點擊牛奶按鈕 */27 - (IBAction)drink {28 [self runAnimationWithName:@"drink" andCount:80];29 }3031 /** 點擊頭部 */32 // 實質是在頭部放置了一個不帶文字的透明按鈕33 - (IBAction)knockHead {34 [self runAnimationWithName:@"knockout" andCount:80];35 }3637 /** 運行相應動畫 */38 - (void) runAnimationWithName:(NSString *) animationName andCount:(int) count {39 if (self.tom.isAnimating) return;4041 NSMutableArray *images = [NSMutableArray array];42 for (int i=0; i <= count; i++) {43 NSString *fileName = [NSString stringWithFormat:@"%@_%02d.jpg", animationName, i];4445 // imageNamed: 有緩存46 // UIImage *image = [UIImage imageNamed:fileName];4748 // imageWithContentOfFile: 沒有緩存(傳入文件的全路徑)49 NSBundle *bundle = [NSBundle mainBundle];50 NSString *path = [bundle pathForResource:fileName ofType:nil];51 UIImage *image = [UIImage imageWithContentsOfFile:path];5253 [images addObject:image];54 }5556 self.tom.animationImages = images; // 存儲了多張組成動畫的圖片5758 [self.tom setAnimationRepeatCount:1]; // 默認0是無限次59 [self.tom setAnimationDuration: images.count/FramesCount];60 [self.tom startAnimating];6162 [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration + 1];63 }646566 @end
新聞熱點
疑難解答