- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我在 swift 中使用容器 View 来获取在导航 Controller 中的多个 View Controller 之间共享的叠加 View 。
它的工作方式是我有许多 subview 为流程创建“背景”。在这些 View 之上,我有一个容器 View ,其中嵌入了一个导航 Controller 。该导航 Controller 通过多个 View Controller 引导用户完成入职流程。在这个容器 View 之上是“进度 View ”,它的前缘与父 View 的前缘对齐,并且随着用户在引导过程中导航而增加宽度,直到最终,栏的宽度等于屏幕的宽度并且用户已经创建了他们的个人资料。这是它的外观示例: Skip to 1:53 to see the main issue
这是包含 View Controller 的样子。注意背景的分层,然后是容器 View ,然后是进度条 View :
这个 View Controller 有一个名为 updateProgressBar
的函数,它获取一定比例的屏幕宽度来设置动画,然后执行动画。
func updateProgressBar(to percentAsDecimal: CGFloat!) {
UIView.animate(withDuration: 0.25) {
self.progressBarWidth.constant = self.view.frame.width * percentAsDecimal
self.view.layoutIfNeeded()
}
}
问题:因为包含的导航 Controller 在进度条的动画期间插入了一个新的 View Controller ,所以当这个新的 View Controller 布置它的 subview 时,这个布置是动画的。
我尝试过的:我创建了自定义类,它们是 UIView 的子类,使用 UIView.performWithoutAnimation
函数来避免这种 subview 动画。
import UIKit
class NonAnimatingView: UIView {
override func layoutSubviews() {
UIView.performWithoutAnimation {
super.layoutSubviews()
}
}
}
我用按钮做了类似的事情。
import UIKit
class NonAnimatingButton: UIButton {
override func layoutSubviews() {
UIView.performWithoutAnimation {
super.layoutSubviews()
}
}
}
但是,当我尝试使用 UISearchBar 执行此操作时,部分动画已解析,但不是全部。
import UIKit
class NonAnimatingSearchBar: UISearchBar {
override func layoutSubviews() {
UIView.performWithoutAnimation {
super.layoutSubviews()
}
}
override func addSubview(_ view: UIView) {
UIView.performWithoutAnimation {
super.addSubview(view)
}
}
override func insertSubview(_ view: UIView, at index: Int) {
UIView.performWithoutAnimation {
super.insertSubview(view, at: index)
}
}
override func insertSubview(_ view: UIView, aboveSubview siblingSubview: UIView) {
UIView.performWithoutAnimation {
super.insertSubview(view, aboveSubview: siblingSubview)
}
}
override func insertSubview(_ view: UIView, belowSubview siblingSubview: UIView) {
UIView.performWithoutAnimation {
super.insertSubview(view, belowSubview: siblingSubview)
}
}
override func sendSubviewToBack(_ view: UIView) {
UIView.performWithoutAnimation {
super.sendSubviewToBack(view)
}
}
override func bringSubviewToFront(_ view: UIView) {
UIView.performWithoutAnimation {
super.bringSubviewToFront(view)
}
}
override func exchangeSubview(at index1: Int, withSubviewAt index2: Int) {
UIView.performWithoutAnimation {
super.exchangeSubview(at: index1, withSubviewAt: index2)
}
}
}
我还确保在推送导航 Controller View Controller 之前调用的任何布局更改都不会被动画化:
override func viewDidLoad() {
UIView.performWithoutAnimation {
super.viewDidLoad()
femaleButton.backgroundColor = UIColor.lightGray.withAlphaComponent(0.25)
maleButton.backgroundColor = UIColor.lightGray.withAlphaComponent(0.25)
otherGenderButton.backgroundColor = UIColor.lightGray.withAlphaComponent(0.25)
femaleButton.layer.cornerRadius = 5
maleButton.layer.cornerRadius = 5
otherGenderButton.layer.cornerRadius = 5
nextButton.layer.cornerRadius = nextButton.frame.height/2
}
}
我想知道的是:有没有我可以调用而不是 UIView.animate
的东西,这样我就可以在指定的 View Controller 中设置动画而不影响当前可见的所有 View Controller ?如果不是,解决此问题的最佳方法是什么?
最佳答案
因此,我最终采纳了@rs7 的建议并改用了CAShapeLayer
。我没有使用界面生成器,而是继续创建一个 CAShapeLayer 并将其添加到 viewDidLayoutSubviews()
var progressBar: CAShapeLayer!
override func viewDidLayoutSubviews() {
progressBar = CAShapeLayer()
progressBar.bounds = CGRect(x: 0, y: 0, width: 0, height: view.safeAreaInsets.top)
progressBar.position = CGPoint(x: 0, y: 0)
progressBar.anchorPoint = CGPoint(x: 0, y: 0)
progressBar.backgroundColor = UIColor.secondaryColor.cgColor
view.layer.addSublayer(progressBar)
}
然后我继续更新 updateProgressBar
以调整 CALayer
的大小,而不是 NSLayoutConstraint
常量。
func updateProgressBar(to percentAsDecimal: CGFloat!) {
let newWidth = view.bounds.width * percentAsDecimal
CATransaction.begin()
CATransaction.setCompletionBlock({
self.progressBar.bounds.size.width = newWidth
})
CATransaction.commit()
let anim = CABasicAnimation(keyPath: "bounds")
anim.isRemovedOnCompletion = false
anim.fillMode = .forwards
anim.duration = 0.25
anim.fromValue = NSValue(cgRect: CGRect(x: 0, y: 0, width: progressBar.bounds.width, height: view.safeAreaInsets.top))
anim.toValue = NSValue(cgRect: CGRect(x: 0, y: 0, width: newWidth, height: view.safeAreaInsets.top))
progressBar.add(anim, forKey: "anim")
}
现在,不再需要我的自定义 NonAnimatingView
、NonAnimatingButton
或 NonAnimatingSearchBar
。 :)
关于ios - UIView 仅在一个 View Controller 中设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56841929/
我有一个 uiview,其中包含其他自定义绘制的 uiview。现在我希望,当用户单击此父 View 时,它将扩展到带有动画的新定义的框架。我使用动画 block 取得了一些成功。问题是展开的 Vie
我正在创建当前显示的 UIView 的屏幕截图。在此过程中,我遇到了控件扭曲和拉伸(stretch)片刻(0.2-0.5 秒)的问题。它仅适用于 iPhone 6、6+。 我的简单代码在这里 (Xam
我想让主 UIView 负责将多个 UIView 之一添加为 subview 。由于任何时候只有一个 subview 处于事件状态,而 subview 完全覆盖了主视图,所以我还不如更换主视图。但这需
这真的只是好奇,但是标签属性有最大值吗?我假设标签是一个 UINT,但我不确定。 最佳答案 “标签”属性是一个 NSInteger。来自 Apple 的 docs : NSInteger Used t
我的布局中有一个 UIView 以便进行一些剪辑和分组,但是自动布局会在缩小时调整它的大小。我想给它一个固定的高度,但唯一的选择是设置顶部和底部空间。 有没有办法设置明确的高度约束? 最佳答案 我刚刚
使用 NSLayoutConstraint类,可以创建基于 View 基线 ( NSLayoutAttributeBaseline ) 的约束。但是,我还没有看到任何描述 UIView 的文档。实际上
我正在尝试创建一个 UIView,它显示一个半透明的圆圈,其边界内有一个不透明的边框。我希望能够通过两种方式更改边界 - 在 -[UIView animateWithDuration:animatio
我目前正在 iOS 中开发一个项目(使用 XCode 和 Swift)。我正在尝试为登录 View 实现以下 UITextFields: 我正在考虑不同的方法来做这件事,但它们看起来都很复杂。如果有人
我正在使用最新的 SDK 开发 iOS 应用程序。 我创建了一个自定义 UIView,我需要向该 UIView 添加一些控件。我也在使用 storyboards,并且我必须向其中添加这个 UIView
我已经通过界面生成器在 super View 中启用了Autoresize Subviews。有没有办法以编程方式禁用一个 subview View 的自动调整大小? 最佳答案 我假设您确实在使用自动
我正在寻找用 Swift 2.0 编写的适用于 iOS 8.0 或更高版本的答案。我有一个圆形的 UIView,我想扩展它但保留圆形的属性。 要设置 UIView,这是代码。 let frame: C
我有一个有趣的问题,但我不确定如何解决它。我正在尝试使用 @IBInspectable 扩展 UIView。但是,使用这种方法,拐角半径似乎是从 nib 文件的默认端设置的,而不是 View 的实际大
我想将 UIView 移动到圆圈内。 UIView 移动圆内的每个点,但不接触圆的边界线。我正在计算圆和 UIView 之间的距离。 var distance = sqrt( pow((tou
A 是一个自定义的 UIView B是UIImagePickerController.view,这个UIImagePickerController.showsCameraControls = NO A
我无法在启动时修改我的 UIView 高度。 我必须使用 UIView,我希望其中一个屏幕大小 * 70,另一个填补空白。 这是我的 @IBOutlet weak var questionFrame
我一直在想这个问题。 我的情况是我需要在 View 中显示很多网格,我有两个计划。 1、在UIView上画很多矩形。我试过了,但是当我放大 View 时,这条线很模糊。 2、给UIView添加很多CA
我可以通过简单地将容器拖动到新的 UIView 来成功地将第一个 UIView 嵌入到容器中,但是要添加过渡 View ,我无法在按键输入事件上用新 View 替换当前的嵌入 View 。 这是我的代
问题是... 我有一条路径,我创建了一个在其中绘制它的 View 。 View 与路径具有相同的维度。然后,我创建第二个 View ,在其中重写 sizeThatFit: 方法,以便缩放第一个 Vie
我正致力于快速设计自定义 UIimageview。我想使用类似于此的 beizerpath 创建一个 UIimageview 编码应该是快速的。任何帮助表示赞赏。谢谢 最佳答案 创建一个 CAShap
我有一个 iOS 应用程序可以在 UIView 上动态绘制形状(通过 drawRect),我正在研究将该应用程序(或其中的一小部分)移植到 Apple Watch 的可能性。不幸的是,在阅读相关帖子后
我是一名优秀的程序员,十分优秀!