美文网首页
汤姆猫App 简单实现

汤姆猫App 简单实现

作者: RWz_my | 来源:发表于2017-07-21 11:15 被阅读13次

源码及素材下载地址: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 简单实现

    源码及素材下载地址:download.csdn.net/detail/rwz_my/9904419 重点在于通过i...

  • 亲子互动中APP推荐(二)

    一、娱乐休闲类 1、Talkingtom cat(汤姆猫) 这在APP应用中应该是最著名的一只猫了,在APP st...

  • 会说话的汤姆猫

    “请问你有看到我的猫吗?他叫汤姆,英国蓝猫。” “汤姆?是动画片《汤姆和杰瑞》里面的汤姆吗?” “是,是,是,就是...

  • 致敬经典——-汤姆猫的自述

    致敬经典——-汤姆猫的自述 题记:汤姆猫和杰瑞鼠今年已经77岁了,汤姆猫说,我老了。 我老了, 没有人来看我。 曾...

  • 缘于汤姆猫,终于汤姆猫

    从母上大人那归家之后,儿子就一直心有不快,属于“没事找事型”,看啥啥都不顺眼,因为母上大人第二天要干活儿,毕...

  • Android实现汤姆猫小游戏

    相信好多人都玩过汤姆猫这个小游戏,要做这个小游戏其实也不难,这不,现写一个,先看效果演示。 目前实现的主要功能点包...

  • 《猫和老鼠》游戏为什么越来越不好玩

    1. “汤姆怎么这么厉害?血太多了,根本打不死嘛。” 老公又在抱怨《猫和老鼠》游戏里的汤姆猫。的确,汤姆猫血很厚,...

  • 汤姆猫

    // // ViewController.m // TOM // // Created by lanou on 1...

  • 汤姆猫

    此代码利用了方法来去避免代码冗余 // // ViewController.m // TomCAt // // C...

  • 汤姆猫

    // // ViewController.m // 汤姆猫 // // Created by lanou on 1...

网友评论

      本文标题:汤姆猫App 简单实现

      本文链接:https://www.haomeiwen.com/subject/ifyrkxtx.html