gpt4 book ai didi

ios - 通过 layoutIfNeeded() 动画特定的约束变化

转载 作者:行者123 更新时间:2023-11-28 13:43:13 32 4
gpt4 key购买 nike

我已经以编程方式构建了 View ,然后为它们设置了动画。有一个 View 动画链,最后一个是要设置动画的按钮。

当我通过调用 layoutIfNeeded() 更改约束(这些约束独立于其他约束并且没有其他 View 与它相关)为按钮设置动画时,一切都工作正常,所有 View 都会更改并转到它们在动画中的初始位置。

我什至尝试了很多其他方法,例如更改 alpha 值等或使其消失。但每一次,它都会改变整个观点。

现在,我无法找出确切的原因。下面是它的代码。

是否可以通过 layoutIfNeeded() 或任何其他方式来更改特定约束来处理此功能?

import UIKit

var nextButton: UIButton!
var nextButtonHeightConstraint: NSLayoutConstraint?
var nextButtonWidthConstraint: NSLayoutConstraint?

override func viewDidLoad() {
super.viewDidLoad()
addNextButton()
}

// ..... function trigerred here.
@IBAction func triggerChange() {
performCaptionAnimation()
}

func addNextButton() {
nextButton = UIButton()
nextButton.translatesAutoresizingMaskIntoConstraints = false
nextButton.clipsToBounds = true
nextButton.setTitle("Next", for: .normal)
nextButton.backgroundColor = .white
nextButton.isUserInteractionEnabled = true
nextButton.titleLabel!.font = AvernirNext(weight: .Bold, size: 16)
nextButton.setTitleColor(.darkGray, for: .normal)
nextButton.roundAllCorners(withRadius: ActionButtonConstraint.height.anchor/2)
nextButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(nextButtonPressed)))
view.addSubview(nextButton)

nextButtonHeightConstraint = nextButton.heightAnchor.constraint(equalToConstant: ActionButtonConstraint.height.anchor)
nextButtonWidthConstraint = nextButton.widthAnchor.constraint(equalToConstant: ActionButtonConstraint.width.anchor)

NSLayoutConstraint.activate([
nextButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: ActionButtonConstraint.right.anchor),
nextButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: ActionButtonConstraint.bottom.anchor),
nextButtonHeightConstraint!, nextButtonWidthConstraint!
])
}

func performCaptionAnimation() {
UIView.animate(withDuration: 0.4, delay: 0.0, options: [.curveEaseInOut], animations: {
self.bannerTwoItemContainer.alpha = 1
self.bannerTwoItemContainer.frame.origin.x -= (self.bannerTwoItemContainer.frame.width/2 + 10)
}) { (done) in
if done {
UIView.animate(withDuration: 0.4, delay: 0.0, options: [.curveEaseInOut], animations: {
self.bannerOneItemContainer.alpha = 1
self.bannerOneItemContainer.frame.origin.x += self.bannerOneItemContainer.frame.width/2 + 10
}) { (alldone) in
if alldone {
// All changes here ......................
self.changeNextButtonContents()
}
}
}
}
}

// ------ This animation has issues and brings all the animation to it's initial point
func changeNextButtonContents() {
self.nextButtonHeightConstraint?.constant = 40
self.nextButtonWidthConstraint?.constant = 110
UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [.curveEaseIn], animations: {
self.nextButton.setTitleColor(.white, for: .normal)
self.nextButton.setTitle("Try Now", for: .normal)
self.nextButton.titleLabel?.font = AvernirNext(weight: .Bold, size: 18)
self.nextButton.backgroundColor = blueColor
self.nextButton.roundAllCorners(withRadius: 20)
self.view.layoutIfNeeded()
}, completion: nil)
}


最佳答案

问题是您的第一个动画会更改其 View 的 frame,如 self.bannerTwoItemContainer.frame.origin.x 中所示。但是您不能更改由约束 定位的事物的框架。如果这样做,那么一旦布局发生,约束就会重新生效。这正是你正在做的,也正是发生的事情。你说 layoutIfNeeded 并且你从未改变的约束被遵守。

重写您的第一个动画以更改 View 的约束,而不是框架,一切都会好起来的。

关于ios - 通过 layoutIfNeeded() 动画特定的约束变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55770000/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com