您的位置:首页 > 产品设计 > UI/UE

UI基础-基础控件-0324-汤姆猫(UIImageView的应用)

2014-11-22 14:57 633 查看
/*
通过tom案例深入了解UIImageView控件的使用及其动画效果
了解UIImage和UIImageView的联系与区别
UIImage的内存优化

在xcode中查看内存方法:地啊你左上方第六个按钮

*/


---------------------

1.images.xcassets中只能放png,至于jpg就放在Supporting Files

查看UIImage和UIImageView的区别
http://blog.csdn.net/baojie1022/article/details/41382925
---------------------

1、UIImageView帧动画的使用

@property(nonatomic,copy) NSArray *animationImages;

需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片)

@property(nonatomic) NSTimeInterval animationDuration;

帧动画的持续时间

@property(nonatomic) NSInteger animationRepeatCount;

帧动画的执行次数(默认是无限循环)

- (void)startAnimating;

开始执行帧动画

- (void)stopAnimating;

停止执行帧动画

- (BOOL)isAnimating;

是否正在执行帧动画

---------------------

2、UIImage的2种加载方式

方式一:有缓存(图片所占用的内存会一直停留在程序中)

+ (UIImage *)imageNamed:(NSString *)name;

name是图片的文件名

方式二:无缓存(图片所占用的内存会在一些特定操作后被清除)

+ (UIImage *)imageWithContentsOfFile:(NSString *)path

- (id)initWithContentsOfFile:(NSString *)path;

path是图片的全路径

---------------------

3、重复代码的封装抽取

当一份代码重复出现在程序的多处地方,就会造成程序又臭又长,当这份代码的结构要修改时,每一处出现这份代码的地方都得修改,导致程序的扩展性很差

因此,要将重复出现的代码抽取到某个方法中,在需要这份代码的地方调用方法即可

抽取代码的思路

将相同的代码放到一个方法中

将不同的值当做方法参数传进来

---------------------

4、文档注释的写法/**注释属性*/

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//  05-汤姆猫
//
// Created by apple on 14-3-24.
// Copyright (c) 2014年 itcast. All rights reserved.
//

#import "MJViewController.h"

@interface MJViewController ()
- (IBAction)drink;
- (IBAction)knock;
- (IBAction)rightFoot;

/* 通过tom案例深入了解UIImageView控件的使用及其动画效果 了解UIImage和UIImageView的联系与区别
UIImage的内存优化

在xcode中查看内存方法:地啊你左上方第六个按钮

*/
/** 这是一只显示图片的猫 */
@property (weak, nonatomic) IBOutlet UIImageView *tom;

@end

@implementation MJViewController
/** 播放动画 */
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
if (self.tom.isAnimating) return;//如果正在播放动画,就不允许进行下面操作

// 1.加载所有的动画图片
NSMutableArray *images = [NSMutableArray array];

for (int i = 0; i<count; i++) {
// 计算文件名
NSString *filename = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];
//%2d,表示有两个占位符,不够的用空格在前方占位,
//%02d ,表示有两个占位符,不够的用0在前方占位,

// 加载图片

// imageNamed: 有缓存(传入文件名)
// UIImage *image = [UIImage imageNamed:filename];

// imageWithContentsOfFile: 没有缓存(传入文件的全路径)
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:filename ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];

// 添加图片到数组中
[images addObject:image];
}

//查看UIImageView中的属性、函数
self.tom.animationImages = images;//UIImageView中有这么一个属性,只允许存放UIImage类型数据

// 2.设置播放次数(1次)
self.tom.animationRepeatCount = 1;

// 3.设置播放时间
self.tom.animationDuration = images.count * 0.05;//让播放变匀速

[self.tom startAnimating];

// 4.动画放完1秒后清除内存:
//使用了  <span style="font-family: Arial, Helvetica, sans-serif;"> imageWithContentsOfFile: 没有缓存(传入文件的全路径)  之后,最后一次的image使用完之后没有释放images,所以需要这么做</span>

CGFloat delay = self.tom.animationDuration + 1.0;
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];//直接在set方法中赋值完成
//    [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
}

//- (void)clearCache
//{
////    self.tom.animationImages = nil;
//
//    [self.tom setAnimationImages:nil];
//}

- (IBAction)drink {
[self runAnimationWithCount:81 name:@"drink"];

//    if (self.tom.isAnimating) return;
//
//    // 1.加载所有的动画图片
//    NSMutableArray *images = [NSMutableArray array];
//
//    for (int i = 0; i<81; i++) {
//        // 计算文件名
//        NSString *filename = [NSString stringWithFormat:@"drink_%02d.jpg", i];
//        // 加载图片
//        UIImage *image = [UIImage imageNamed:filename];
//        // 添加图片到数组中
//        [images addObject:image];
//    }
//    self.tom.animationImages = images;
//
//    // 2.设置播放次数(1次)
//    self.tom.animationRepeatCount = 1;
//
//    // 3.设置播放时间
//    self.tom.animationDuration = images.count * 0.05;
//
//    [self.tom startAnimating];
}

- (IBAction)knock {
[self runAnimationWithCount:81 name:@"knockout"];
}

- (IBAction)rightFoot {
[self runAnimationWithCount:30 name:@"footRight"];
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: