[iOS学习]iOS Tom猫工程

2016-09-07  本文已影响0人  AmazingMiracle
@implementation ViewController

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //导入本地文件“tom.plist”
    NSString *filename = [[NSBundle mainBundle]pathForResource:@"tom.plist" ofType:nil];
    _dicData = [NSDictionary dictionaryWithContentsOfFile:filename];
}

- (void)runAnimation:(NSString *)actiontype withNumber:(NSInteger)number
{
    //判断当前是否有动画正在进行
    if (_pictureImageView.isAnimating == NO)
    {
        NSMutableArray *images = [[NSMutableArray alloc]init];
        for (int i = 0; i < number; i++)
        {
            NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg",actiontype ,i];
            //有缓存的一种图片加载方式,直接将图片全部加载在内存中

//            UIImage *image = [UIImage imageNamed:filename];

            //这样加载是将图片加载在系统文件中需要时调入内存
            NSString *filepath = [[NSBundle mainBundle] pathForResource:filename ofType:nil];
            NSData *data = [NSData dataWithContentsOfFile:filepath];
            UIImage *image = [UIImage imageWithData:data];
            [images addObject:image];
        }
        _pictureImageView.animationImages = images;
        _pictureImageView.animationDuration = number * 0.07;
        _pictureImageView.animationRepeatCount = 1;
        [_pictureImageView startAnimating];
        //在动画结束0.5秒之后将内存释放
        [_pictureImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:_pictureImageView.animationDuration + 0.5];
    }
}

- (IBAction)eatButton:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"eat"] integerValue];
    [self runAnimation:@"eat" withNumber:number];
}

- (IBAction)dringButton:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"drink"] integerValue];
    [self runAnimation:@"drink" withNumber:number];
}

- (IBAction)cymbalButton:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"cymbal"] integerValue];
    [self runAnimation:@"cymbal" withNumber:number];
}

- (IBAction)fartButton:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"fart"] integerValue];
    [self runAnimation:@"fart" withNumber:number];
}

- (IBAction)pieButton:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"pie"] integerValue];
    [self runAnimation:@"pie" withNumber:number];
}

- (IBAction)scratchButton:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"scratch"] integerValue];
    [self runAnimation:@"scratch" withNumber:number];
}

- (IBAction)leftFoot:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"foot_right"] integerValue];
    [self runAnimation:@"footRight" withNumber:number];
}

- (IBAction)rightFoot:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"foot_left"] integerValue];
    [self runAnimation:@"footLeft" withNumber:number];
}

- (IBAction)knock:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"knockout"] integerValue];
    [self runAnimation:@"knockout" withNumber:number];
}

- (IBAction)stomach:(id)sender {
    NSInteger number = [[_dicData objectForKey:@"stomach"] integerValue];
    [self runAnimation:@"stomach" withNumber:number];
}
@end
上一篇下一篇

猜你喜欢

热点阅读