POP Animation 和 layoutSubviews 的冲突

简介: 问题现象因为 Facebook 的 POP 框架用起来很舒服,于是一直慢慢来习惯了用 POP 做动画,最近做了一个很简单的让一个 Button 旋转的动画,程序却异常的崩溃了,崩溃的地方在 -layoutSubviews 这个地方,如下图目测,应该是...

问题现象

因为 Facebook 的 POP 框架用起来很舒服,于是一直慢慢来习惯了用 POP 做动画,最近做了一个很简单的让一个 Button 旋转的动画,程序却异常的崩溃了,崩溃的地方在 -layoutSubviews 这个地方,如下图目测,应该是因为动画的时候,触发了 -layoutSubviews 方法,于是崩溃,就像这样

img_476fdbf4437eed827cf5b41dfb3243ac.png
Crash

并且在终端输出了这样的信息

Jun 27 07:05:17  shenzhenren[5868] <Error>: CGAffineTransformInvert: singular matrix.
Jun 27 07:05:17  shenzhenren[5868] <Error>: CGAffineTransformInvert: singular matrix.

原因分析

我们先来看下关键的几处代码是怎么写的:

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.isShowAll = NO;
        [self loadShowAllButton];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.showAllButton.top = 8.0f;
    self.showAllButton.right = self.width;
}

- (void)loadShowAllButton {
    self.showAllButton = [[UIButton alloc] init];
    [self addSubview:self.showAllButton];
    self.showAllButton.width = 36.0f;
    self.showAllButton.height = 36.0f;
    self.showAllButton.left = 0.0f;
    self.showAllButton.top = 0.0f;
    self.showAllButton.contentMode = UIViewContentModeCenter;
    [self.showAllButton setImage:[UIImage imageNamed:@"default-show-more-arrow"] forState:UIControlStateNormal];
    [self.showAllButton addTarget:self action:@selector(showAllButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)showAllButtonPushed:(UIButton *)sender {
    self.isShowAll = !self.isShowAll;
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerRotation];
    if (self.isShowAll) {
        animation.toValue = @M_PI;
    } else {
        animation.toValue = @0;
    }
    [sender pop_removeAllAnimations];
    [sender pop_addAnimation:animation forKey:nil];
}

上面的代码并不复杂,我想要实现的东西就是在点击按钮的时候,让他旋转180度,再次点击的时候让他转回来

那么根据上面代码,崩溃的过程其实是这样的:

  1. 点击按钮,触发 POP 动画
  2. POP 的动画为 UIView 添加对应的 Transform,同时产生动画
  3. 因为 UIView 添加了 Transform,所以 UIView 触发了需要重新计算 layout 的过程
  4. 调用 - (void)layoutSubviews 重新计算布局
  5. 因为 UIView 有 Transform 了,再通过 -setFrame:(GRect)frame 设置 UIView 的 frame 出现错误,参考官方文档

如何解决

既然找到了原因,那么我们就根据原因来跳坑吧,既然是触发了 View 重新计算布局,那么我们不要让他在这个父 View 触发即可,那么最简单粗暴的解决方法就是,创建一个新的 View 包裹住我们需要动画的 View 于是,这样修改

- (void)layoutSubviews {
    [super layoutSubviews];
    self.showAllButtonContainerView.top = 8.0f;
    self.showAllButtonContainerView.right = self.width;
}

- (void)loadShowAllButton {
    self.showAllButtonContainerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 36.0f, 36.0f)];
    [self addSubview:self.showAllButtonContainerView];
    self.showAllButton = [[UIButton alloc] init];
    [self.showAllButtonContainerView addSubview:self.showAllButton];
    self.showAllButton.width = 36.0f;
    self.showAllButton.height = 36.0f;
    self.showAllButton.left = 0.0f;
    self.showAllButton.top = 0.0f;
    self.showAllButton.contentMode = UIViewContentModeCenter;
    [self.showAllButton setImage:[UIImage imageNamed:@"default-show-more-arrow"] forState:UIControlStateNormal];
    [self.showAllButton addTarget:self action:@selector(showAllButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
}

其实问题的原因还是没有好好阅读官方文档

目录
相关文章
|
Ubuntu 关系型数据库 MySQL
M1 macos docker获取x86 x64 amd 等指定架构版本linux ubuntu mysql 容器并启动容器
M1 macos docker获取x86 x64 amd 等指定架构版本linux ubuntu mysql 容器并启动容器
|
Windows
使用dumpbin工具查看动态库中函数列表
使用dumpbin工具查看动态库中函数列表
3466 0
|
消息中间件 XML JSON
一文就读懂RPC远程调用核心原理
rpc的全称是Remote Procedure Call,即远程过程调用,是分布式系统的常用通信方法。 Remote,简单来说的话就是两个不同的服务之间,两个服务肯定是两个不同的进程。因此,我们就从跨进程进行访问的角度去理解就行了。 Procedure,意思是一串可执行的代码,我们写Java的方法,就是一段课程行的代码。 Call,即调用,调用的就是跨了进程的方法。
998 0
一文就读懂RPC远程调用核心原理
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8066 2
|
10天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18440 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1493 5