iOS OC C

简易游戏

会说话的Tom猫

920 发布: 2015/11/16 12:55 本文总阅读量
#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *catImageView;

- (IBAction)doCymbal:(UIButton *)sender;

- (IBAction)doDrink:(UIButton *)sender;

- (IBAction)doEat:(UIButton *)sender;

- (IBAction)doFart:(UIButton *)sender;

- (IBAction)doPie:(UIButton *)sender;

- (IBAction)doScratch:(UIButton *)sender;

@end
@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)beginAnimationWithCount:(int)count andName:(NSString*)name {

    if (_catImageView.isAnimating) {

        [_catImageView stopAnimating];
    }

    //接收图片

    NSMutableArray* image_array = [NSMutableArray array];

    for (int i=0; i<=count; i++) {

        NSString* imageName = [NSString stringWithFormat:@"%@_d.jpg",name,i];
        //有缓存

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

        //没有缓存
        NSString*path=[[NSBundle mainBundle]pathForResource:imageName ofType:nil];
        UIImage*image=[UIImage imageWithContentsOfFile:path];
        [image_array addObject:image];

    }

    _catImageView.animationImages = image_array;//展示动画
    _catImageView.animationDuration = [image_array count]*0.1;
    _catImageView.animationRepeatCount = 1;
    [_catImageView startAnimating];

    //一次动画后释放内存

    [_catImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:[image_array count]*0.1];
}

- (IBAction)doCymbal:(UIButton *)sender {

    [self beginAnimationWithCount:12 andName:@"cymbal"];

}

- (IBAction)doDrink:(UIButton *)sender{

    [self beginAnimationWithCount:80 andName:@"drink"];

}

- (IBAction)doEat:(UIButton *)sender{

    [self beginAnimationWithCount:39 andName:@"eat"];

}

- (IBAction)doFart:(UIButton *)sender{

    [self beginAnimationWithCount:27 andName:@"fart"];

}

- (IBAction)doPie:(UIButton *)sender{

    [self beginAnimationWithCount:23 andName:@"pie"];

}

- (IBAction)doScratch:(UIButton *)sender{

    [self beginAnimationWithCount:55 andName:@"scratch"];

}

@end