阿里云服务器免费领卷啦。

捡代码论坛-最全的游戏源码下载技术网站!

 找回密码
 立 即 注 册

QQ登录

只需一步,快速开始

搜索
关于源码区的附件失效或欺骗帖, 处理办法
查看: 5815|回复: 7

[ios源码] 适合iOS开发,入门练手精品小项目--(1)Tom猫源码下载

[复制链接]

4213

主题

210

回帖

12万

积分

管理员

管理员

Rank: 9Rank: 9Rank: 9

积分
127401
QQ
发表于 2016-8-8 10:05:23 | 显示全部楼层 |阅读模式
  一直以来想着,整理一下学习iOS开发的经历.借着这段空闲的时间,特意为想入门iOS开发,或者刚入门的新手,提供一些练手的小项目.
    Tom猫游戏,想必大家都很熟悉,各种版本的Tom猫.别看着这样一个小小的游戏,想要把它做成比较完美的项目,必须要多思考.比如说:1.界面的合理搭建;2.代码的封装性;3.CPU利用率;4.内存的优化.
    根据以上的几个侧重点,重写了Tom猫这个小项目,希望能对新手们有所帮助.大神们么井喷,本人实属入门级别而已.

(1)延时加载技术,也叫懒加载.提高CPU的利用率,加快启动速度.
  1. - (NSDictionary *)imgs{

  2. if (!_imgs) {

  3. NSString *path = [[NSBundle mainBundle] pathForResource:@"tom.plist" ofType:nil];

  4. NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

  5. _imgs = dict;

  6. }

  7. return _imgs;

  8. }
复制代码
(2)代码的封装:注意设置控件的tag值时,最后使用100以后的值,避免与Xcode内部系统产生冲突.


  1. - (IBAction)clickBtn:(UIButton *)sender {

  2. //    NSLog(@"sender.tag = %ld",(long)sender.tag);

  3. //根据绑定的tag值,来进行判断

  4. switch (sender.tag) {

  5. case 101:

  6. {

  7. int count = [self.imgs[@"pie"] intValue];

  8. [self playAnimationWithCount:count andPrefixName:@"pie"];

  9. }

  10. break;

  11. case 102:

  12. {

  13. int count = [self.imgs[@"cymbal"] intValue];

  14. [self playAnimationWithCount:count andPrefixName:@"cymbal"];

  15. }

  16. break;

  17. case 103:

  18. {

  19. int count = [self.imgs[@"drink"] intValue];

  20. [self playAnimationWithCount:count andPrefixName:@"drink"];

  21. }

  22. break;

  23. case 104:

  24. {

  25. int count = [self.imgs[@"eat"] intValue];

  26. [self playAnimationWithCount:count andPrefixName:@"eat"];

  27. }

  28. break;

  29. case 105:

  30. {

  31. int count = [self.imgs[@"fart"] intValue];

  32. [self playAnimationWithCount:count andPrefixName:@"fart"];

  33. }

  34. break;

  35. case 106:

  36. {

  37. int count = [self.imgs[@"scratch"] intValue];

  38. [self playAnimationWithCount:count andPrefixName:@"scratch"];

  39. }

  40. break;

  41. case 107:

  42. {

  43. int count = [self.imgs[@"knockout"] intValue];

  44. [self playAnimationWithCount:count andPrefixName:@"knockout"];

  45. }

  46. break;

  47. case 108:

  48. {

  49. int count = [self.imgs[@"angry"] intValue];

  50. [self playAnimationWithCount:count andPrefixName:@"angry"];

  51. }

  52. break;

  53. case 109:

  54. {

  55. int count = [self.imgs[@"stomach"] intValue];

  56. [self playAnimationWithCount:count andPrefixName:@"stomach"];

  57. }

  58. break;

  59. case 110:

  60. {

  61. int count = [self.imgs[@"foot_right"] intValue];

  62. [self playAnimationWithCount:count andPrefixName:@"footRight"];

  63. }

  64. break;

  65. case 111:

  66. {

  67. int count = [self.imgs[@"foot_left"] intValue];

  68. [self playAnimationWithCount:count andPrefixName:@"footLeft"];

  69. }

  70. break;

  71. default:

  72. break;

  73. }

  74. }
复制代码
(3)设置序列帧动画效果:注意图片无缓存加载方式,以及图片对象数组的及时释放.


  1. - (void)playAnimationWithCount:(int)count andPrefixName:(NSString *)name{

  2. if(self.imgView.isAnimating){

  3. return;

  4. }

  5. NSMutableArray *imgArray = [NSMutableArray array];

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

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

  8. NSString *imgPath = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];

  9. UIImage *image = [UIImage imageWithContentsOfFile:imgPath];

  10. UIImage *newImage = [self getImage:image andSize:[UIScreen mainScreen].bounds.size];

  11. [imgArray addObject:newImage];

  12. }

  13. self.imgView.animationImages = imgArray;

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

  15. self.imgView.animationRepeatCount = 1;

  16. [self.imgView startAnimating];

  17. //必须把创建的数组,在播放动画完成后,置为nil,进行释放.

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

  19. }
复制代码
(4)开辟子线程对图片进行重采集工作,减轻主线程的任务,加快速度,以及优化内存.

  1. - (UIImage *)getImage:(UIImage *)image andSize:(CGSize)size{

  2. UIGraphicsBeginImageContext(size);

  3. CGRect imageRect = CGRectMake(0, 0, size.width, size.height);

  4. [image drawInRect:imageRect];

  5. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

  6. UIGraphicsEndImageContext();

  7. return newImage;

  8. }
复制代码
预览Tom猫项目的效果:

113.png rr00349.png



总结:每写一个新的项目,或者老项目,其实知识点都是在逐渐累加的过程.新的方法,新的技术,新的优化效果等等,都是在实践操作,查资料,看博客等中获取到的.编程之路,就是一条学习之路.


源码下载地址:
游客,如果您要查看本帖隐藏内容请回复









捡代码论坛-最全的游戏源码下载技术网站! - 论坛版权郑重声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与捡代码论坛-最全的游戏源码下载技术网站!享有帖子相关版权
3、捡代码论坛版权,详细了解请点击。
4、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
5、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。 我们不承担任何技术及版权问题,且不对任何资源负法律责任。
6、如无法链接失效或侵犯版权,请给我们来信:jiandaima@foxmail.com

回复

使用道具 举报

0

主题

6

回帖

42

积分

新手上路

Rank: 1

积分
42
发表于 2016-8-9 17:09:46 | 显示全部楼层
登录可见评论
回复

使用道具 举报

0

主题

206

回帖

1033

积分

金牌会员

Rank: 6Rank: 6

积分
1033
发表于 2016-11-9 09:20:38 | 显示全部楼层
登录可见评论
回复

使用道具 举报

0

主题

62

回帖

296

积分

中级会员

Rank: 3Rank: 3

积分
296
发表于 2017-1-5 13:39:40 | 显示全部楼层
登录可见评论
回复

使用道具 举报

0

主题

21

回帖

57

积分

注册会员

Rank: 2

积分
57
发表于 2017-1-12 15:22:35 | 显示全部楼层
登录可见评论
回复

使用道具 举报

1

主题

66

回帖

795

积分

高级会员

Rank: 4

积分
795
发表于 2017-1-16 10:24:25 | 显示全部楼层
登录可见评论
回复

使用道具 举报

0

主题

7

回帖

48

积分

新手上路

Rank: 1

积分
48
发表于 2017-12-22 11:33:06 | 显示全部楼层
登录可见评论
回复

使用道具 举报

0

主题

1

回帖

14

积分

新手上路

Rank: 1

积分
14
发表于 2019-7-23 16:43:51 | 显示全部楼层
登录可见评论
回复

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立 即 注 册

本版积分规则

技术支持
在线咨询
QQ咨询
3351529868

QQ|手机版|小黑屋|捡代码论坛-专业源码分享下载 ( 陕ICP备15015195号-1|网站地图

GMT+8, 2025-2-22 16:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表