当前位置: 首页 > news >正文

UI-定时器与动画使用总结

#pragma mark - 定时器   ************************************************************************************

   //0.创建一个以下延时使用的方法

    - (void)delayMethod { NSLog(@"execute"); }

    1.performSelector方法================================================================

    [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0f];

    此方式要求必须在主线程中执行,否则无效。

    是一种非阻塞的执行方式,

    暂时未找到取消执行的方法。   

 

    2.定时器:NSTimer==================================================================

     创建定时器

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(delayMethod ) userInfo:nil repeats:NO];

    把定时器 添加到当前主运行循环中

    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    

    下面这种方法创建的定时器,会自动的加入运行循环

    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];

    此方式要求必须在主线程中执行,否则无效。

    是一种非阻塞的执行方式,

    可以通过NSTimer类的- (void)invalidate;取消执行。    

 

    3. sleep方式==============================================================================

    sleep(1.0f);    

    [NSThread sleepForTimeInterval:1.0f]; [self delayMethod];

    此方式在主线程和子线程中均可执行。

    是一种阻塞的执行方式,建方放到子线程中,以免卡住界面

    没有找到取消执行的方法。

    

    4.GCD方式  ===========================================================================  

    double delayInSeconds = 1.0;

    __block ViewController* bself = self;

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [bself delayMethod]; });

    此方式在可以在参数中选择执行的线程。

    是一种非阻塞的执行方式,

    没有找到取消执行的方法。

 

#pragma mark - 1. 动画************************************************************************************

    一. 通过定时播放多张图片动画=====================================================

    1.1 定义 UIImageView

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    

    1.2 定义 NSMutableArray 保存所有图片

    NSMutableArray *animationImgsArrM = [[NSMutableArray alloc] init];

    int num = 40;

    for(int i = 0 ; i <= num  ; i++ ){

        图片的名称

        NSString *imgName = [NSString stringWithFormat:@"%@_%02d.jpg", imgView, i ];

        在创建比较小的,并且常用的图片时,可以使用imageNamed:方法创建

        UIImage *img = [UIImage imageNamed:imgName];

        

        如果创建的图片很多,并且比较大的话,应该使用这种方式创建

        NSString *filePath = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];

        UIImage *img = [[UIImage alloc]initWithContentsOfFile:filePath];

        把图片添加到数组中

        [animationImgsArrM addObject:img];

    }

   

    设置动画属性

    imgView.animationImages = animationImgsArrM;    //动画内容

    imgView.animationDuration = num * 0.08;         //动画(总)时间间隔

    imgView.animationRepeatCount = 1;               //动画重复次数

 ..............

    

    开始动画

    [imgView startAnimating];

    

    动画播放完成后,清除数组中的图片

    1.  [self performSelector:@selector(clearImages) withObject:nil afterDelay:imgView.animationDuration];

    2.  [imgView performSelector:@selector(setAnimationImages:) withObject:nil

                   afterDelay:imgView.animationDuration];    

  动画播放完成后,清除数组中的图片。        让imgView控件在动画完成后执行setAnimationImages:方法,该方法默认参数为nil

    二.通过定时播放较少图片动画 , 通常在改变某一控件的frame时使用===================================

    UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

    imgView1.image = [UIImage imageNamed:@"head"];

     1. 开始动画

    [UIView beginAnimations:nil context:nil];

    2. 动画持续2秒

    [UIView setAnimationDuration:2.0];    

    2.1 取出原来的属性

    CGPoint tempCenter = imgView1.center;

    2.2 修改临时属性

    tempCenter.y -= 200;

    2.3 重新赋值

    imgView1.center = tempCenter; 

    3. 提交动画

    [UIView commitAnimations];

    

    三. 通过block 创建动画 可以通过[UIView anima...]设置想要设置的某种动画=========================

    

    [UIView animateWithDuration:2 animations:^{

        

        imgView1.transform = CGAffineTransformMakeTranslation(0, 100 );

    }];

    [UIView animateWithDuration:<#(NSTimeInterval)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>];   

 

转载于:https://www.cnblogs.com/GJ-ios/p/5373443.html

相关文章:

  • J2EE 课件2
  • Oracle SQL monitor
  • HTML 利用MAP创建图片中的链接的映射
  • CSS选择器(二)
  • 2016.04.14,英语,《Vocabulary Builder》Unit 14
  • 实验三 白盒测试
  • php 实现简单的登录
  • 一百多套开发视频教程的下载地址
  • jquery中attr和prop的区别
  • 利用qmake生成Makefile文件
  • 结对编程——关于Fault、Error、Failure程序设计
  • 流程控制
  • javascript之数组操作
  • Vmware复制或克隆Linux系统后找不到eth0的解决方案
  • Git(三):Git 使用规范流程
  • 【comparator, comparable】小总结
  • 【翻译】babel对TC39装饰器草案的实现
  • 11111111
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • JavaScript 基础知识 - 入门篇(一)
  • nginx 配置多 域名 + 多 https
  • Nodejs和JavaWeb协助开发
  • springMvc学习笔记(2)
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • Vue小说阅读器(仿追书神器)
  • WePY 在小程序性能调优上做出的探究
  • 猴子数据域名防封接口降低小说被封的风险
  • 类orAPI - 收藏集 - 掘金
  • 判断客户端类型,Android,iOS,PC
  • 使用parted解决大于2T的磁盘分区
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 在electron中实现跨域请求,无需更改服务器端设置
  • 我们雇佣了一只大猴子...
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #define
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • ${factoryList }后面有空格不影响
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (JS基础)String 类型
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (离散数学)逻辑连接词
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (南京观海微电子)——COF介绍
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (学习日记)2024.01.19
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (一)认识微服务