ios学习03-制作简单的tom猫

学习了一个小时时间的tom猫小游戏,主要是学习UIImageview的动画属性,现在记录下新学习到的知识点。

主要知识点有:

1.UIImageview的各种动画方法设置

2.NSMutableArray

3.UIImage imageNamed:imageName 和 [UIImage imageWithContentsOfFile:path]的区别


界面就是我们以前玩过的tom猫游戏,点击它的一些位置,会表现不同的动作,这其实用到的是UIImageview的连续图片显示动画的知识点,代码如下:

#import "ViewController.h"

@interface ViewController ()

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

@end

@implementation ViewController

- (void)toAnimation:(NSString *)animateName count:(int)count {
    if (self.tom.isAnimating) {
        return;
    }
    NSMutableArray *images = [NSMutableArray array];
    for (int i=0; i<count; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",animateName,i];
        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [images addObject:image];
    }
    [self.tom setAnimationImages:images];
    [self.tom setAnimationDuration:images.count * 0.075];
    [self.tom startAnimating];
    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
}

- (IBAction)drink:(id)sender {
    [self toAnimation:@"drink" count:81];
}


@end

UIImageview里有animation的数组,我们要做的就是 设置图片数组,然后开启UIImageview的动画,期间还要考虑到内存销毁等。

学习到的新东西:

1.UIImageview的performSelector方法,作用时延迟动画播放完毕后,销毁图片占用内存的操作。

2.UIImage imageNamed:imageName 图片使用完成后,不会立即销毁,等待系统来统一销毁,适合通篇比较下并且少的情况。

[UIImage imageWithContentsOfFile:path],通过路径来寻找图片,适合与图片比较多的情况。这里先通过名字参数找到文件名字,通过文件名字找到路径,通过路径初始化图片。

3.NSMutableArray是NSArray的子类,有add和delete方法,类似java里的List类。

4.string类型的占位符是: %@

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值