ฉันได้สร้างมุมมองโดยทางโปรแกรมแล้วสร้างภาพเคลื่อนไหว มีภาพเคลื่อนไหวแบบลูกโซ่และอันสุดท้ายคือปุ่มที่จะเคลื่อนไหว
ในขณะที่ทุกสิ่งทำงานได้ดีทันทีที่ฉันทำให้ปุ่มเคลื่อนไหวโดยการเปลี่ยนข้อจำกัด (ข้อจำกัดเหล่านี้ไม่ขึ้นอยู่กับสิ่งอื่นและไม่มีมุมมองอื่นใดที่เกี่ยวข้องกับมัน) โดยการเรียกรูปแบบ IfNeeded() มุมมองทั้งหมดจะเปลี่ยนและไปที่ตำแหน่งเริ่มต้น ของภาพเคลื่อนไหว
ฉันยังลองใช้วิธีอื่นๆ มากมาย เช่น การเปลี่ยนค่าอัลฟ่า ฯลฯ หรือทำให้มันหายไป แต่ทุกครั้งมันเปลี่ยนมุมมองทั้งหมด
ตอนนี้ฉันไม่สามารถหาสาเหตุที่แท้จริงได้ ด้านล่างนี้เป็นรหัสของมัน
มีวิธีเปลี่ยนข้อจำกัดเฉพาะด้วย 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)
}