iOS 您所在的位置:网站首页 ios项目实验报告 iOS

iOS

2023-07-15 13:31| 来源: 网络整理| 查看: 265

功能分析 点击对应的按钮后,让汤姆猫展现对应的动画

步骤分析 1、搭建UI界面 2、监听按钮点击 3、根据点击的按钮执行对应的动画

知识点: 1、UIImageView帧动画的使用 2、UIImage的2种加载方式 3、重复代码的封装抽取 4、文档注释的写法

UIImageView帧动画相关属性和方法 @property(nonatomic,copy) NSArray *animationImages; 需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片) @property(nonatomic) NSTimeInterval animationDuration; 帧动画的持续时间 @property(nonatomic) NSInteger animationRepeatCount; 帧动画的执行次数(默认是无限循环) - (void)startAnimating; 开始执行帧动画 - (void)stopAnimating; 停止执行帧动画 - (BOOL)isAnimating; 是否正在执行帧动画

UIImage的2种加载方式 方式一:有缓存(图片所占用的内存会一直停留在程序中) + (UIImage )imageNamed:(NSString )name; name是图片的文件名

方式二:无缓存(图片所占用的内存会在一些特定操作后被清除) + (UIImage )imageWithContentsOfFile:(NSString )path - (id)initWithContentsOfFile:(NSString *)path; path是图片的全路径 方式二对于内存更优化

重复代码的封装抽取: 1、当一份代码重复出现在程序的多处地方,就会造成程序又臭又长,当这份代码的结构要修改时,每一处出现这份代码的地方都得修改,导致程序的扩展性很差 2、因此,要将重复出现的代码抽取到某个方法中,在需要这份代码的地方调用方法即可

抽取代码的思路 1.将相同的代码放到一个方法中 2。将不同的值当做方法参数传进来

代码简摘:(不拖控件,使用纯代码大家界面)

#import "HMViewController.h" @interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *tom; @end @implementation HMViewController /** 重构代码: 1、将重复的代码复制到新方法中 2、根据需要调整方法 关于图像实例化 UIImage UIImageView imageName: 系统推荐使用,但是图像实例化之后的释放由系统负责 如果要自己释放图片不能使用imageName方法! UIImage *image = [UIImage imageNamed:imageName]; 取而代之的方法: [UIImage imageWithContentsOfFile:] 注意:一遇到ContentsOfFile则必须使用全路径!! 提示:如果放在Images.xcassets 中的图片(存放经常使用的图片),不能使用imageWithContentsOfFile: 临时使用的大图片放在Supporting Files */ -(void)tomAnimationWithName:(NSString *)name count:(NSInteger)count{ //判断是否在动画 if([self.tom isAnimating]) return; //动画图片数组 NSMutableArray *imageArray = [NSMutableArray array]; int i; for (i = 0 ; i< count ; i++) { NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",name,i]; //UIImage *image = [UIImage imageNamed:imageName]; NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil]; UIImage *image = [UIImage imageWithContentsOfFile:path]; [imageArray addObject:image]; } //设置动画数组 self.tom.animationImages = imageArray; //重复一次 self.tom.animationRepeatCount = 1; //动画时长 self.tom.animationDuration = self.tom.animationImages.count * 0.075; //开始动画 [self.tom startAnimating]; // //点击事件结束以后释放数组 // self.tom.animationImages = nil; [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration]; } //currentTitle 可以去除按钮当前标题文字 -(IBAction)tomAction:(UIButton *)button{ [self tomAnimationWithName:button.currentTitle count:button.tag]; } - (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. } @end

运行结果截图: 开始界面

这里写图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有