เคลื่อนไหวการเปลี่ยนแปลงข้อจำกัดเฉพาะตามเค้าโครงถ้าจำเป็น()

ฉันได้สร้างมุมมองโดยทางโปรแกรมแล้วสร้างภาพเคลื่อนไหว มีภาพเคลื่อนไหวแบบลูกโซ่และอันสุดท้ายคือปุ่มที่จะเคลื่อนไหว

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



person Aakash Dave    schedule 20.04.2019    source แหล่งที่มา
comment
คุณไม่ได้เคลื่อนไหวข้อจำกัด แต่เป็นเฟรม   -  person Inder Kumar Rathore    schedule 20.04.2019
comment
@InderKumarRathore ???? ขอบคุณเพื่อน   -  person Aakash Dave    schedule 20.04.2019


คำตอบ (1)


ปัญหาคือภาพเคลื่อนไหวแรกของคุณเปลี่ยน เฟรม ของมุมมอง เช่นเดียวกับใน self.bannerTwoItemContainer.frame.origin.x แต่คุณไม่สามารถเปลี่ยนกรอบของสิ่งที่อยู่ในตำแหน่งโดย ข้อจำกัด หากคุณทำเช่นนั้น ทันทีที่เค้าโครงเกิดขึ้น ข้อจำกัดก็จะยืนยันอีกครั้ง นั่นคือสิ่งที่คุณกำลังทำและสิ่งที่เกิดขึ้น คุณพูดว่า layoutIfNeeded และข้อจำกัดที่คุณไม่เคยเปลี่ยนแปลงจะถูกปฏิบัติตาม

เขียนแอนิเมชั่นแรกของคุณใหม่เพื่อเปลี่ยนข้อจำกัดของมุมมอง ไม่ใช่เฟรม และทุกอย่างจะเรียบร้อยดี

person matt    schedule 20.04.2019
comment
ฉันชอบพูดว่า: กรอบและข้อจำกัดเป็นศัตรูกัน ในทางเทคนิคเพิ่มเติม: ข้อจำกัดเป็นเพียงรายการคำสั่ง และรูปแบบหมายถึงการปฏิบัติตามคำสั่งเหล่านั้น และมันทำอย่างนั้นได้อย่างไร? ด้วยการเปลี่ยนเฟรม! - person matt; 20.04.2019
comment
สิ่งที่ควรให้ความสำคัญโดยทั่วไป ข้อจำกัด(ฉันคิดว่านี่) หรือเฟรมขณะวาดหรือเคลื่อนไหว - person Aakash Dave; 20.04.2019
comment
ภาพเคลื่อนไหวแบบเฟรมสามารถใช้ได้สำหรับภาพเคลื่อนไหวชั่วคราว แต่ข้อจำกัดจะชนะในที่สุด ดังนั้นในกรณีนี้ หากการจัดเรียงใหม่นี้เป็นเพียงชั่วคราว บางทีคุณอาจทำทุกอย่างด้วยเฟรมได้ ไม่เช่นนั้นคุณจะต้องหาวิธีทำทุกอย่างที่มีข้อจำกัด - person matt; 20.04.2019
comment
ตกลง. เนื่องจากนี่เป็นเพียงภาพเคลื่อนไหวชั่วคราว ฉันจึงควรลบข้อจำกัดของปุ่มออก แทนที่จะเพิ่มข้อจำกัดให้กับมุมมองอื่นๆ - person Aakash Dave; 20.04.2019