湯姆貓實現程式碼
阿新 • • 發佈:2019-02-09
//
// ViewController.m
// Tom
//
// Created by administrator on 15/8/4.
// Copyright (c) 2015年 gengcong. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *tom;
- (IBAction)drinkClick:(id)sender;
- (IBAction)peiClick:(id )sender;
- (IBAction)eatClick:(id)sender;
- (IBAction)cymbalClick:(id)sender;
- (IBAction)scratchClick:(id)sender;
- (IBAction)fart:(id)sender;
- (IBAction)stomach:(id)sender;
- (IBAction)footleft:(id)sender;
- (IBAction)footright:(id)sender;
- (IBAction)knockoutClick:(id)sender;
- (IBAction )angryClick:(id)sender;
@end
@implementation ViewController
- (IBAction)drinkClick:(id)sender {
[self runAnimationWithName:@"drink" andcount:81];
}
- (IBAction)peiClick:(id)sender {
[self runAnimationWithName:@"pie" andcount:24];
}
- (IBAction)eatClick:(id)sender {
[self runAnimationWithName:@"eat" andcount:40];
}
- (IBAction)cymbalClick:(id)sender {
[self runAnimationWithName:@"cymbal" andcount:13];
}
- (IBAction)scratchClick:(id)sender {
[self runAnimationWithName:@"scratch" andcount:56];
}
- (IBAction)fart:(id)sender {
[self runAnimationWithName:@"fart" andcount:28];
}
- (IBAction)stomach:(id)sender {
[self runAnimationWithName:@"stomach" andcount:34];
}
- (IBAction)footleft:(id)sender {
[self runAnimationWithName:@"footRight" andcount:30];
}
- (IBAction)footright:(id)sender {
[self runAnimationWithName:@"footLeft" andcount:30];
}
- (IBAction)knockoutClick:(id)sender {
[self runAnimationWithName:@"knockout" andcount:81];
}
- (IBAction)angryClick:(id)sender {
[self runAnimationWithName:@"angry" andcount:26];
}
-(void)runAnimationWithName:(NSString*)name andcount:(int)counts
{
if (self.tom.isAnimating) return;
NSMutableArray *images=[[NSMutableArray alloc]init];
for (int i=0; i<counts; i++) {
NSString *thanten=[[NSString alloc]init];
if (i<10){
thanten=[NSString stringWithFormat:@"0%d",i];
}
else{
thanten=[NSString stringWithFormat:@"%d",i];
}
NSString *filename=[NSString stringWithFormat:@"%@_%@.jpg",name,thanten];
UIImage *image=[UIImage imageNamed:filename ];
[images addObject:image];
}
// 設定動畫的圖片來源
self.tom.animationImages=images;
// 設定播放次數
self.tom.animationRepeatCount=1;
//設定動畫播放的時間
self.tom.animationDuration=images.count*0.07;
[self.tom startAnimating];
//設定延時清除記憶體
CGFloat delay=self.tom.animationDuration +1.0;
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
}
//-(void)clean
//{
// self.tom.animationImages=nil;
// //[self.tom setAnimationImages:nil];
//
//}
@end