如图所示:通过触摸不同的按钮显示不同的图片动画 按钮文字信息 将文字颜色设置为隐藏的
#import <UIKit/UIKit.h> @interface WHBLAPViewController : UIViewController @property (weak, nonatomic) IBOutlet UIImageView *tom; // tom猫 - (IBAction)btnClick:(UIButton *)sender; // 视图中的所有button都和此方法绑定监听 @end
#import "WHBLAPViewController.h" @interface WHBLAPViewController () { NSDictionary *_dict; // 保存所有图片的个数 } @end @implementation WHBLAPViewController - (void)viewDidLoad { [super viewDidLoad]; // 1.获得tom.plist的全路径 NSBundle *bundle = [NSBundle mainBundle]; NSString *path = [bundle pathForResource:@"tom" ofType:@"plist"]; // 2.根据文件路径加载字典 _dict = [NSDictionary dictionaryWithContentsOfFile:path]; } #pragma mark 监听所有的按钮点击 - (IBAction)btnClick:(UIButton *)sender { // 1.如果tom正在播放动画,直接返回 if (_tom.isAnimating) return; // 2.取出按钮文字 NSString *title = [sender titleForState:UIControlStateNormal]; // 3.获得图片数量 int count = [_dict[title] intValue]; // 4.播放动画 [self playAnim:count fliename:title]; } - (void)playAnim:(int)count fliename:(NSString *)filename { // 1.创建可变数组 NSMutableArray *images = [NSMutableArray array]; // 2.添加图片 for (int i = 0; i<count; i++) { // 图片名 NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg", filename, i]; // 全路径 NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil]; UIImage *img = [[UIImage alloc] initWithContentsOfFile:path]; // 1.有缓存(无法释放,参数传的是文件名) [UIImage imageNamed:@""]; 2.无缓存(用完就会释放,参数传的是全路径) [[UIImage alloc] initWithContentsOfFile:]; [images addObject:img]; } // 3.设置动画图片(有顺序) _tom.animationImages = images;// 序列帧动画 // 4.只播放一次 _tom.animationRepeatCount = 1; // 5.设置动画的持续时间 _tom.animationDuration = 0.1 * count; // 5.开始动画 [_tom startAnimating]; } @end
附录
Plist文件 Plist文件,又称属性列表是一种XML格式的文件 可以保存NSString、NSNumber、NSDate和NSData四种数据类型的数据 可以与NSArray和NSDictionary组合使用建立数组或者数据字典