CATransaction(参考其他博客敲)

简介:

#import "ViewController.h"
 
@interface ViewController ()
@property(nonatomic,strong)CALayer *calayer;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame=CGRectMake(100, 100, 100, 50) ;
    btn.tag=1;
    [btn setTitle:@"隐式事务按钮" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setTintColor:[UIColor blackColor]];
    [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
     
    UIButton *btn1 =[UIButton buttonWithType:UIButtonTypeSystem];
    btn1.frame=CGRectMake(200, 100, 100, 50) ;
    btn1.tag=2;
    [btn1 setTitle:@"显式事务按钮" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn1 setTintColor:[UIColor blackColor]];
    [btn1 addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
//    CATransaction 事务类,可以对多个layer的属性同时进行修改.它分隐式事务,和显式事务.
//    区分隐式动画和隐式事务:隐式动画通过隐式事务实现动画 。修改Calayer的属性属于隐式事务
//    区分显式动画和显式事务:显式动画有多种实现方式,显式事务是一种实现显式动画的方式。
     //隐式事务
    self.calayer=[CALayer layer];
    self.calayer.bounds=CGRectMake(150, 150, 100, 100);
    self.calayer.position=CGPointMake(100, 200);
    self.calayer.backgroundColor=[UIColor redColor].CGColor;
    self.calayer.borderColor = [UIColor blackColor].CGColor;
    self.calayer.opacity = 1.0f;
    [self.view.layer addSublayer:self.calayer];
    [super viewDidLoad];
 
}
-(void)btnclick:(id)sender
{
    UIButton *btn=(UIButton *)sender;
    if (btn.tag==1) {
        [CATransaction setDisableActions:YES];
        self.calayer.cornerRadius = (self.calayer.cornerRadius == 0.0f) ? 30.0f : 0.0f;
        self.calayer.opacity = (self.calayer.opacity == 1.0f) ? 0.5f : 1.0f;
    }
    else
    {
        //事务嵌套
        [CATransaction begin];
        [CATransaction begin];
        [CATransaction setDisableActions:YES];
        self.calayer.opacity = (self.calayer.opacity == 1.0f) ? 0.5f : 1.0f;
        [CATransaction commit];
        //上面的动画并不会立即执行,需要等最外层的commit
        [NSThread sleepForTimeInterval:3];
        //显式事务默认开启动画效果,kCFBooleanTrue关闭
        [CATransaction setValue:(id)kCFBooleanFalse
                         forKey:kCATransactionDisableActions];
        //动画执行时间
        [CATransaction setValue:[NSNumber numberWithFloat:10.0f] forKey:kCATransactionAnimationDuration];
        //[CATransaction setAnimationDuration:[NSNumber numberWithFloat:5.0f]];
        self.calayer.cornerRadius = (self.calayer.cornerRadius == 0.0f) ? 30.0f : 0.0f;
        [CATransaction commit];
         
    }
     
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end

相关文章
|
JSON 数据库 C++
Rapidjson的使用过程-Parse解析数组遇到的问题,附自己的解决方式
关于RapidJSON,网上有很多资料,RapidJSON是腾讯开源的一个高效的C++ JSON解析器及生成器,它是只有头文件的C++库。RapidJSON是跨平台的,支持Windows, Linux, Mac OS X及iOS, Android。它的源码在https://github.com/Tencent/rapidjson/。这里也不过多介绍如何使用RapidJson,网上有很多如何使用,只介绍自己使用过程中遇到的问题,及其解决问题的方式。
622 0
完美解决->“pip : 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。”
完美解决->“pip : 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。”
完美解决->“pip : 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。”
|
传感器 数据采集 物联网
基于STM32的光敏传感器数据采集系统-嵌入式系统与设计课程设计2
基于STM32的光敏传感器数据采集系统-嵌入式系统与设计课程设计
1771 0
|
Kubernetes 负载均衡 调度
在K8S中,如何实现集群管理?
在K8S中,如何实现集群管理?
|
图形学 开发者 存储
超越基础教程:深度拆解Unity地形编辑器的每一个隐藏角落,让你的游戏世界既浩瀚无垠又细节满满——从新手到高手的全面技巧升级秘籍
【8月更文挑战第31天】Unity地形编辑器是游戏开发中的重要工具,可快速创建复杂多变的游戏环境。本文通过比较不同地形编辑技术,详细介绍如何利用其功能构建广阔且精细的游戏世界,并提供具体示例代码,展示从基础地形绘制到植被与纹理添加的全过程。通过学习这些技巧,开发者能显著提升游戏画面质量和玩家体验。
928 3
|
运维 安全 测试技术
自动化运维的利剑:Ansible在企业级部署中的应用与挑战
本文深入探讨了Ansible,这一领先的IT自动化工具,如何在企业级部署中扮演关键角色。我们将通过实际案例分析,揭示Ansible在简化配置管理、加速应用部署和提高运维效率方面的优势。同时,文章也将不回避Ansible实施过程中可能遇到的技术挑战与限制,并提供针对性的解决策略。阅读本文后,您将获得一个全面的视角,理解Ansible在现代企业运维中不可或缺的地位,以及如何克服其面临的主要问题。
333 1
|
机器学习/深度学习 移动开发 JavaScript
Web实时通信的学习之旅:SSE(Server-Sent Events)的技术详解及简单示例演示
Web实时通信的学习之旅:SSE(Server-Sent Events)的技术详解及简单示例演示
5744 0
|
自然语言处理 测试技术 计算机视觉
ICLR 2024:Time-LLM:基于大语言模型的时间序列预测
【2月更文挑战第28天】ICLR 2024:Time-LLM:基于大语言模型的时间序列预测
2348 1
ICLR 2024:Time-LLM:基于大语言模型的时间序列预测
|
Linux
linux centos6.8系统配置网络yum源
linux centos6.8系统配置网络yum源
979 0
|
安全 Linux 网络安全
ssh中的密码登录和密钥登录
ssh中的密码登录和密钥登录