ios - 在 Swift 中用动画改变 UIView 的超级视图
问题描述
我有一个VideoView
(它是 的子视图UIView
)。
默认情况下,它被添加到UIView
屏幕角落的小视图中(我称之为ParrentView1
)。
我有一个缩小按钮VideoView
。此按钮执行从更大的视图中删除VideoView
并将ParentView1
其添加到更大视图(称为ParrentView2
)的操作。
当我执行下面的代码时,它可以工作,但动画很奇怪。ParrentView1
我所需要的只是从to缩小动画ParrentView2
。这是我的代码:
VideoView.removeFromSuperview()
ParrentView2.addSubview(VideoView)
UIView.animate(withDuration: 0.8, delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseOut,
animations: {
VideoView.frame = ParrentView2.bounds
}, completion: nil)
谢谢你的帮助
解决方案
可能的原因是当将它添加到另一个视图时,它被分配了一个不同的框架。解决方案是确保动画从原始位置开始。就像是:
CGRect originalRect = ParrentView2.convert(VideoView.frame, from:VideoView.superView);
VideoView.removeFromSuperview()
ParrentView2.addSubview(VideoView)
VideoView.frame = originalRect;
UIView.animate(withDuration: 0.8, delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseOut,
animations: {
VideoView.frame = ParrentView2.bounds
}, completion: nil)
一个改进点:请注意,在 Swift 中习惯上以小写字母开头变量名。当他们不这样做时,它会变得混乱。
推荐阅读
- mysql - 安装mysql时文件丢失
- python - 如何使用 Flask-Migrate 轻松更改列?
- sql - 以纳秒为单位的时间戳列上的 Hive 窗口函数范围
- mysql - 当一条路线当前正在向数据库插入大量数据时,如何使所有其他路线正常工作?
- java - 如何在 TranslateAnimation 中获得位置?
- angular - 减少高图表中 X 轴上的数据点数量
- node.js - 错误 [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main 在 /app/node_modules/@babel/helper-compilation-targets/package.json 中解析
- javascript - React 中的条件类名?
- angular - 如何在 docker env 中使用 ngnix 反向代理通信 UI 和后端应用程序
- python - Python NameError - 名称未定义