首页 > 移动平台 > 详细

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

时间:2015-05-19 14:46:23      阅读:262      评论:0      收藏:0      [点我收藏+]

学习了一个小时时间的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类型的占位符是: %@

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

原文:http://blog.csdn.net/u013173289/article/details/45843755

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!