汤姆猫App 简单实现

源码及素材下载地址:download.csdn.net/detail/rwz_my/9904419

重点在于通过imageNamed加载图片与通过图片路径取图片的区别

//  ViewController.m

//  汤姆猫

//

//  Created by Ren on 17/5/16.

//  Copyright © 2017年 Ren. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

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

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

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

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

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

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

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

- (IBAction)knockout:(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)startAnimation:(NSString *)picName indexCount:(int)indexCount;

{

//判断动画是否在执行

if (self.imgView.isAnimating)

{

return;

}

//1.创建一个存放图片的数组

NSMutableArray *imgArr = [NSMutableArray arrayWithCapacity:0];

for (int i = 0 ; i < indexCount; i++)

{

NSString *imgName = [NSString stringWithFormat:@"%@_%02d",picName,i];

/*

通过imageNamed加载图片

优点:图片加载到内存当中后,下次调用时直接取出,无需加载,不会释放

缺点:如果需要加载的图片过多,会导致占用非常大部分的内存,甚至会直接被系统强制退出程序

*/

//        UIImage *img = [UIImage imageNamed:imgName];

NSString *path = [[NSBundle mainBundle] pathForResource:imgName ofType:@"jpg"];

//通过图片路径取图片,需要传递一个图片的完整路径,适合加载大量图片

UIImage *img = [UIImage imageWithContentsOfFile:path];

[imgArr addObject:img];

}

//2.将图片存入到序列图片数组中

self.imgView.animationImages = imgArr;

//3.设置动画持续时间;

self.imgView.animationDuration = imgArr.count * 0.1;

//4.设置动画循环次数(默认为无数次)

self.imgView.animationRepeatCount = 1;

//5.开启动画

[self.imgView startAnimating];

//6.清空图片数组--performSelector方法:延迟执行;

//                  @selector(setAnimationImages:) 当这个方法启动后

//                  withObject: 设置为nil后,self.imgView中就被清空

//                    afterDelay:延迟时间

[self.imgView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:imgArr.count * 0.1];

}

- (IBAction)drink:(UIButton *)sender

{

[self startAnimation:@"drink" indexCount:80];

}

- (IBAction)pi:(UIButton *)sender

{

[self startAnimation:@"fart" indexCount:26];

}

- (IBAction)eat:(UIButton *)sender

{

[self startAnimation:@"eat" indexCount:38];

}

- (IBAction)za:(UIButton *)sender

{

[self startAnimation:@"footLeft" indexCount:28];

}

- (IBAction)zhua:(UIButton *)sender

{

[self startAnimation:@"footRight" indexCount:28];

}

- (IBAction)qiao:(UIButton *)sender

{

[self startAnimation:@"cymbal" indexCount:11];

}

- (IBAction)knockout:(UIButton *)sender

{

[self startAnimation:@"knockout" indexCount:80];

}

@end

你可能感兴趣的:(汤姆猫App 简单实现)