失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Tom汤姆猫的简单实现

Tom汤姆猫的简单实现

时间:2023-06-19 08:30:20

相关推荐

Tom汤姆猫的简单实现

//

// ViewController.m

// TomCat

//

// Created by 黄松凯 on 15/2/17.

// Copyright (c) SK. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

- (IBAction)drink;

- (IBAction)head;

- (IBAction)cymbal;

- (IBAction)fart;

- (IBAction)scratch;

- (IBAction)eat;

- (IBAction)pie;

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

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

}

//将方法进行封装,可以

-(void)runAnimationWithCount:(int)count name:(NSString *)name

{

if(self.tom.isAnimating)//如果正在喝牛奶,则不要打断

return;

//加载所有的动画图片

NSMutableArray *images=[NSMutableArray array];

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

{

//文件名

NSString *filename=[NSString stringWithFormat:@"%@_%02d.jpg",name,i];

//加载图片(这种方法有缓存,不利用内存优化)

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

//添加图片到数组中

//[images addObject:image];

NSBundle *bundle=[NSBundle mainBundle];

NSString *path=[bundle pathForResource:filename ofType:nil];

UIImage *image=[UIImage imageWithContentsOfFile:path];

[images addObject:image];

}

self.tom.animationImages=images;

//设置播放次数

self.tom.animationRepeatCount=1;

//设置播放时间

self.tom.animationDuration=2.0;

//开始播放

[self.tom startAnimating];

CGFloat delay=self.tom.animationDuration+1;

[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];

}

- (IBAction)drink

{

[self runAnimationWithCount:81 name:@"drink"];

}

- (IBAction)head {

[self runAnimationWithCount:81 name:@"knockout"];

}

- (IBAction)cymbal {

[self runAnimationWithCount:13 name:@"cymbal"];

}

- (IBAction)fart {

[self runAnimationWithCount:28 name:@"fart"];

}

- (IBAction)scratch {

[self runAnimationWithCount:56 name:@"scratch"];

}

- (IBAction)eat {

[self runAnimationWithCount:40 name:@"eat"];

}

- (IBAction)pie {

[self runAnimationWithCount:24 name:@"pie"];

}

@end

如果觉得《Tom汤姆猫的简单实现》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。